From 1d6aac7839afc5ed9bfdccba5747f20c325e71af Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Thu, 10 Jul 2014 18:08:52 +0400 Subject: [PATCH] Fixes in triple-address code parser, 1% tests passed --- Makefile | 10 ++++----- src/error.c | 27 ---------------------- src/generated.h | 2 +- src/libjsparser/lexer.c | 22 +++++++++--------- src/libjsparser/lexer.h | 4 +++- src/libjsparser/mappings.h | 2 +- src/libjsparser/parser.c | 46 +++++++++++++++++++++++++------------- src/main.c | 43 ++++++++++++++++++++++++++++++----- src/pretty-printer.c | 2 +- 9 files changed, 90 insertions(+), 68 deletions(-) delete mode 100644 src/error.c diff --git a/Makefile b/Makefile index 38d0b2845..4f58e5efb 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ SUP_STM32F4 = ./third-party/STM32F4-Discovery_FW_V1.1.0/Libraries/CMSIS/ST/STM32 # Add common-io.c and sensors.c SOURCES = \ $(sort \ - $(wildcard ./src/jerry-libc.c ./src/pretty-printer.c ./src/error.c) \ + $(wildcard ./src/jerry-libc.c ./src/pretty-printer.c) \ $(wildcard ./src/libperipherals/actuators.c) \ $(wildcard ./src/libjsparser/*.c) \ $(wildcard ./src/libecmaobjects/*.c) \ @@ -81,10 +81,10 @@ STRIP = strip # General flags CFLAGS ?= $(INCLUDES) -std=c99 #-fdiagnostics-color=always -#CFLAGS += -Wall -Wextra -Wpedantic -Wlogical-op -Winline -#CFLAGS += -Wformat-nonliteral -Winit-self -Wstack-protector -#CFLAGS += -Wconversion -Wsign-conversion -Wformat-security -#CFLAGS += -Wstrict-prototypes -Wmissing-prototypes +CFLAGS += -Wall -Wextra -Wpedantic -Wlogical-op -Winline +CFLAGS += -Wformat-nonliteral -Winit-self -Wstack-protector +CFLAGS += -Wconversion -Wsign-conversion -Wformat-security +CFLAGS += -Wstrict-prototypes -Wmissing-prototypes # Flags for MCU MCU_CFLAGS += -mlittle-endian -mcpu=cortex-m4 -march=armv7e-m -mthumb diff --git a/src/error.c b/src/error.c deleted file mode 100644 index 5a9690581..000000000 --- a/src/error.c +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright 2014 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "error.h" - -extern void lexer_dump_buffer_state (void); - -void -fatal (int code) -{ - printf ("FATAL: %d\n", code); - lexer_dump_buffer_state (); - JERRY_UNREACHABLE (); - exit (code); -} diff --git a/src/generated.h b/src/generated.h index fcd3b82d5..c45b513df 100644 --- a/src/generated.h +++ b/src/generated.h @@ -13,7 +13,7 @@ * limitations under the License. */ -static const char* generated_source = "" +static const char* generated_source __unused = "" "while (true) {\n" "LEDToggle (LED3);\n" "LEDToggle (LED6);\n" diff --git a/src/libjsparser/lexer.c b/src/libjsparser/lexer.c index f8051e1ff..69d1e8e32 100644 --- a/src/libjsparser/lexer.c +++ b/src/libjsparser/lexer.c @@ -87,11 +87,11 @@ is_empty (token tok) return tok.type == TOK_EMPTY; } -#ifdef JERRY_NDEBUG +#ifdef __HOST FILE *lexer_debug_log; #endif -#ifdef JERRY_NDEBUG +#ifdef __HOST static FILE *file; static char *buffer_start; @@ -169,7 +169,7 @@ static const char *token_start; #define LA(I) (*(buffer + I)) -#endif // JERRY_NDEBUG +#endif // __HOST /* If TOKEN represents a keyword, return decoded keyword, if TOKEN represents a Future Reserved Word, return KW_RESERVED, @@ -230,7 +230,7 @@ current_token (void) { JERRY_ASSERT (buffer); JERRY_ASSERT (token_start); - JERRY_ASSERT (token_start > buffer); + JERRY_ASSERT (token_start <= buffer); size_t length = (size_t) (buffer - token_start); char *res = (char *) malloc (length + 1); strncpy (res, token_start, length); @@ -578,7 +578,7 @@ grobble_whitespaces (void) } } -#ifdef JERRY_NDEBUG +#ifdef __HOST void lexer_set_file (FILE *ex_file) { @@ -634,7 +634,7 @@ replace_comment_by_newline (void) } } -#ifdef JERRY_NDEBUG +#ifdef __HOST static token lexer_next_token_private (void) #else @@ -675,7 +675,7 @@ lexer_next_token (void) { grobble_whitespaces (); return -#ifdef JERRY_NDEBUG +#ifdef __HOST lexer_next_token_private (); #else lexer_next_token (); @@ -688,7 +688,7 @@ lexer_next_token (void) return (token) { .type = TOK_NEWLINE, .data.none = NULL }; else return -#ifdef JERRY_NDEBUG +#ifdef __HOST lexer_next_token_private (); #else lexer_next_token (); @@ -699,7 +699,7 @@ lexer_next_token (void) { replace_comment_by_newline ();; return -#ifdef JERRY_NDEBUG +#ifdef __HOST lexer_next_token_private (); #else lexer_next_token (); @@ -771,7 +771,7 @@ lexer_next_token (void) fatal (ERR_NON_CHAR); } -#ifdef JERRY_NDEBUG +#ifdef __HOST static int i = 0; token @@ -793,7 +793,7 @@ lexer_next_token (void) void lexer_save_token (token tok) { - #ifdef JERRY_NDEBUG + #ifdef __HOST // if (tok.type == TOK_CLOSE_BRACE) fprintf (lexer_debug_log, "lexer_save_token(%d): type=0x%x, data=%p\n", i, tok.type, tok.data.none); #endif diff --git a/src/libjsparser/lexer.h b/src/libjsparser/lexer.h index b3335d5df..2c30ad288 100644 --- a/src/libjsparser/lexer.h +++ b/src/libjsparser/lexer.h @@ -151,7 +151,7 @@ typedef struct } token; -#ifdef JERRY_NDEBUG +#ifdef __HOST void lexer_set_file (FILE *); #else void lexer_set_source (const char *); @@ -159,4 +159,6 @@ void lexer_set_source (const char *); token lexer_next_token (void); void lexer_save_token (token); +void lexer_dump_buffer_state (void); + #endif \ No newline at end of file diff --git a/src/libjsparser/mappings.h b/src/libjsparser/mappings.h index 28face982..6536bca0e 100644 --- a/src/libjsparser/mappings.h +++ b/src/libjsparser/mappings.h @@ -16,7 +16,7 @@ #ifndef MAPPINGS_H #define MAPPINGS_H -#ifndef JERRY_NDEBUG +#ifndef __HOST #include "../jerry-libc.h" #include "allocator.h" #include diff --git a/src/libjsparser/parser.c b/src/libjsparser/parser.c index c689f2e58..62f6a6693 100644 --- a/src/libjsparser/parser.c +++ b/src/libjsparser/parser.c @@ -16,6 +16,18 @@ #include "parser.h" #include "error.h" #include "lexer.h" +#include "error.h" + +extern void lexer_dump_buffer_state (void); + +void +fatal (int code) +{ + printf ("FATAL: %d\n", code); + lexer_dump_buffer_state (); + JERRY_UNREACHABLE (); + exit (code); +} bool is_formal_parameter_list_empty (formal_parameter_list list) @@ -68,7 +80,7 @@ is_statement_null (statement stmt) static token tok; -#ifdef JERRY_NDEBUG +#ifdef __HOST FILE *debug_file; #endif @@ -133,14 +145,14 @@ current_scope_must_be (unsigned int scopes) static inline void current_scope_must_be_global (void) { - if (scope_index != 0) + if (scope_index != 1) fatal (ERR_PARSER); } static void push_scope (int type) { -#ifdef JERRY_NDEBUG +#ifdef __HOST fprintf (debug_file, "push_scope: 0x%x\n", type); #endif current_scopes[scope_index++] = (scope) { .type = type, .was_stmt = false }; @@ -149,7 +161,7 @@ push_scope (int type) static void pop_scope (void) { -#ifdef JERRY_NDEBUG +#ifdef __HOST fprintf (debug_file, "pop_scope: 0x%x\n", current_scopes[scope_index - 1].type); #endif scope_index--; @@ -160,7 +172,7 @@ assert_keyword (keyword kw) { if (tok.type != TOK_KEYWORD || tok.data.kw != kw) { -#ifdef JERRY_NDEBUG +#ifdef __HOST printf ("assert_keyword: 0x%x\n", kw); #endif JERRY_UNREACHABLE (); @@ -178,7 +190,7 @@ current_token_must_be(token_type tt) { if (tok.type != tt) { -#ifdef JERRY_NDEBUG +#ifdef __HOST printf ("current_token_must_be: 0x%x\n", tt); #endif fatal (ERR_PARSER); @@ -199,7 +211,7 @@ next_token_must_be (token_type tt) tok = lexer_next_token (); if (tok.type != tt) { -#ifdef JERRY_NDEBUG +#ifdef __HOST printf ("next_token_must_be: 0x%x\n", tt); #endif fatal (ERR_PARSER); @@ -786,7 +798,7 @@ parse_operator: res.type = ET_PROP_REF; skip_newlines (); res.data.ops.op2 = parse_operand (); - assert (!res.data.ops.op2.is_literal); + JERRY_ASSERT (!res.data.ops.op2.is_literal); return res; case TOK_OPEN_SQUARE: @@ -962,7 +974,7 @@ parse_for_or_for_in_statement (void) fatal (ERR_PARSER); } } - assert (is_variable_declaration_empty(list.decls[0])); + JERRY_ASSERT (is_variable_declaration_empty(list.decls[0])); /* expression contains left_hand_side_expression. */ expr = parse_expression (); @@ -981,13 +993,13 @@ plain_for: res.is_for_in = false; if (!is_variable_declaration_empty(list.decls[0])) { - assert (is_expression_empty(expr.exprs[0])); + JERRY_ASSERT (is_expression_empty(expr.exprs[0])); res.data.for_stmt.init.is_decl = true; res.data.for_stmt.init.data.decl_list = list; } if (!is_expression_empty(expr.exprs[0])) { - assert (is_variable_declaration_empty(list.decls[0])); + JERRY_ASSERT (is_variable_declaration_empty(list.decls[0])); res.data.for_stmt.init.is_decl = false; res.data.for_stmt.init.data.expr = expr; } @@ -1015,14 +1027,14 @@ for_in: res.is_for_in = true; if (!is_variable_declaration_empty(list.decls[0])) { - assert (is_expression_empty (expr.exprs[0])); - assert (is_variable_declaration_empty (list.decls[1])); + JERRY_ASSERT (is_expression_empty (expr.exprs[0])); + JERRY_ASSERT (is_variable_declaration_empty (list.decls[1])); res.data.for_in_stmt.init.is_decl = true; res.data.for_in_stmt.init.data.decl = list.decls[0]; } if (!is_expression_empty(expr.exprs[0])) { - assert (is_expression_empty(expr.exprs[0])); + JERRY_ASSERT (is_expression_empty(expr.exprs[0])); res.data.for_in_stmt.init.is_decl = false; res.data.for_in_stmt.init.data.left_hand_expr = expr.exprs[0]; } @@ -1112,6 +1124,8 @@ parser_parse_statement (void) statement res; res.data.none = NULL; + JERRY_ASSERT (scope_index); + skip_newlines (); if (is_keyword (KW_FINALLY)) @@ -1374,8 +1388,8 @@ parser_parse_statement (void) void parser_init (void) { - scope_index = 0; -#ifdef JERRY_NDEBUG + scope_index = 1; +#ifdef __HOST debug_file = fopen ("parser.log", "w"); #endif } diff --git a/src/main.c b/src/main.c index aed89170f..6f990bc4a 100644 --- a/src/main.c +++ b/src/main.c @@ -13,7 +13,7 @@ * limitations under the License. */ -#ifdef JERRY_NDEBUG +#ifdef __HOST #include #include #include @@ -39,6 +39,10 @@ #include "generated.h" +#include "lexer.h" +#include "parser.h" +#include "pretty-printer.h" + void fake_exit (void); void @@ -95,10 +99,11 @@ fake_exit (void) int main (int argc, char **argv) { + statement st; bool dump_tokens = false; - bool dump_ast = true; + bool dump_ast = false; const char *file_name = NULL; -#ifdef JERRY_NDEBUG +#ifdef __HOST FILE *file = NULL; #endif @@ -124,16 +129,44 @@ main (int argc, char **argv) if (dump_tokens && dump_ast) fatal (ERR_SEVERAL_FILES); -#ifdef JERRY_NDEBUG + if (!dump_tokens) + dump_ast = true; + +#ifdef __HOST file = fopen (file_name, "r"); if (file == NULL) { fatal (ERR_IO); } + + lexer_set_file (file); +#else + lexer_set_source (generated_source); #endif - TODO(Call parser); + if (dump_ast) + { + parser_init (); + st = parser_parse_statement (); + JERRY_ASSERT (!is_statement_null (st)); + while (st.type != STMT_EOF) + { + pp_statement (st); + st = parser_parse_statement (); + JERRY_ASSERT (!is_statement_null (st)); + } + } + + if (dump_tokens) + { + token tok = lexer_next_token (); + while (tok.type != TOK_EOF) + { + pp_token (tok); + tok = lexer_next_token (); + } + } //gen_bytecode (generated_source); //gen_bytecode (); diff --git a/src/pretty-printer.c b/src/pretty-printer.c index b79ab3ac6..6ad6d151a 100644 --- a/src/pretty-printer.c +++ b/src/pretty-printer.c @@ -548,7 +548,7 @@ dump_postfix (operand op, const char *operation) static void pp_assignment_expression (assignment_expression expr) { - if (expr.var) + if (expr.oper != AO_NONE && expr.var) printf ("%s", expr.var); switch (expr.oper)