Fixes in triple-address code parser, 1% tests passed

This commit is contained in:
Ilmir Usmanov
2014-07-10 18:08:52 +04:00
parent 7da95ecefa
commit 1d6aac7839
9 changed files with 90 additions and 68 deletions
+5 -5
View File
@@ -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 # Add common-io.c and sensors.c
SOURCES = \ SOURCES = \
$(sort \ $(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/libperipherals/actuators.c) \
$(wildcard ./src/libjsparser/*.c) \ $(wildcard ./src/libjsparser/*.c) \
$(wildcard ./src/libecmaobjects/*.c) \ $(wildcard ./src/libecmaobjects/*.c) \
@@ -81,10 +81,10 @@ STRIP = strip
# General flags # General flags
CFLAGS ?= $(INCLUDES) -std=c99 #-fdiagnostics-color=always CFLAGS ?= $(INCLUDES) -std=c99 #-fdiagnostics-color=always
#CFLAGS += -Wall -Wextra -Wpedantic -Wlogical-op -Winline CFLAGS += -Wall -Wextra -Wpedantic -Wlogical-op -Winline
#CFLAGS += -Wformat-nonliteral -Winit-self -Wstack-protector CFLAGS += -Wformat-nonliteral -Winit-self -Wstack-protector
#CFLAGS += -Wconversion -Wsign-conversion -Wformat-security CFLAGS += -Wconversion -Wsign-conversion -Wformat-security
#CFLAGS += -Wstrict-prototypes -Wmissing-prototypes CFLAGS += -Wstrict-prototypes -Wmissing-prototypes
# Flags for MCU # Flags for MCU
MCU_CFLAGS += -mlittle-endian -mcpu=cortex-m4 -march=armv7e-m -mthumb MCU_CFLAGS += -mlittle-endian -mcpu=cortex-m4 -march=armv7e-m -mthumb
-27
View File
@@ -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);
}
+1 -1
View File
@@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
static const char* generated_source = "" static const char* generated_source __unused = ""
"while (true) {\n" "while (true) {\n"
"LEDToggle (LED3);\n" "LEDToggle (LED3);\n"
"LEDToggle (LED6);\n" "LEDToggle (LED6);\n"
+11 -11
View File
@@ -87,11 +87,11 @@ is_empty (token tok)
return tok.type == TOK_EMPTY; return tok.type == TOK_EMPTY;
} }
#ifdef JERRY_NDEBUG #ifdef __HOST
FILE *lexer_debug_log; FILE *lexer_debug_log;
#endif #endif
#ifdef JERRY_NDEBUG #ifdef __HOST
static FILE *file; static FILE *file;
static char *buffer_start; static char *buffer_start;
@@ -169,7 +169,7 @@ static const char *token_start;
#define LA(I) (*(buffer + I)) #define LA(I) (*(buffer + I))
#endif // JERRY_NDEBUG #endif // __HOST
/* If TOKEN represents a keyword, return decoded keyword, /* If TOKEN represents a keyword, return decoded keyword,
if TOKEN represents a Future Reserved Word, return KW_RESERVED, if TOKEN represents a Future Reserved Word, return KW_RESERVED,
@@ -230,7 +230,7 @@ current_token (void)
{ {
JERRY_ASSERT (buffer); JERRY_ASSERT (buffer);
JERRY_ASSERT (token_start); JERRY_ASSERT (token_start);
JERRY_ASSERT (token_start > buffer); JERRY_ASSERT (token_start <= buffer);
size_t length = (size_t) (buffer - token_start); size_t length = (size_t) (buffer - token_start);
char *res = (char *) malloc (length + 1); char *res = (char *) malloc (length + 1);
strncpy (res, token_start, length); strncpy (res, token_start, length);
@@ -578,7 +578,7 @@ grobble_whitespaces (void)
} }
} }
#ifdef JERRY_NDEBUG #ifdef __HOST
void void
lexer_set_file (FILE *ex_file) lexer_set_file (FILE *ex_file)
{ {
@@ -634,7 +634,7 @@ replace_comment_by_newline (void)
} }
} }
#ifdef JERRY_NDEBUG #ifdef __HOST
static token static token
lexer_next_token_private (void) lexer_next_token_private (void)
#else #else
@@ -675,7 +675,7 @@ lexer_next_token (void)
{ {
grobble_whitespaces (); grobble_whitespaces ();
return return
#ifdef JERRY_NDEBUG #ifdef __HOST
lexer_next_token_private (); lexer_next_token_private ();
#else #else
lexer_next_token (); lexer_next_token ();
@@ -688,7 +688,7 @@ lexer_next_token (void)
return (token) { .type = TOK_NEWLINE, .data.none = NULL }; return (token) { .type = TOK_NEWLINE, .data.none = NULL };
else else
return return
#ifdef JERRY_NDEBUG #ifdef __HOST
lexer_next_token_private (); lexer_next_token_private ();
#else #else
lexer_next_token (); lexer_next_token ();
@@ -699,7 +699,7 @@ lexer_next_token (void)
{ {
replace_comment_by_newline ();; replace_comment_by_newline ();;
return return
#ifdef JERRY_NDEBUG #ifdef __HOST
lexer_next_token_private (); lexer_next_token_private ();
#else #else
lexer_next_token (); lexer_next_token ();
@@ -771,7 +771,7 @@ lexer_next_token (void)
fatal (ERR_NON_CHAR); fatal (ERR_NON_CHAR);
} }
#ifdef JERRY_NDEBUG #ifdef __HOST
static int i = 0; static int i = 0;
token token
@@ -793,7 +793,7 @@ lexer_next_token (void)
void void
lexer_save_token (token tok) lexer_save_token (token tok)
{ {
#ifdef JERRY_NDEBUG #ifdef __HOST
// if (tok.type == TOK_CLOSE_BRACE) // 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); fprintf (lexer_debug_log, "lexer_save_token(%d): type=0x%x, data=%p\n", i, tok.type, tok.data.none);
#endif #endif
+3 -1
View File
@@ -151,7 +151,7 @@ typedef struct
} }
token; token;
#ifdef JERRY_NDEBUG #ifdef __HOST
void lexer_set_file (FILE *); void lexer_set_file (FILE *);
#else #else
void lexer_set_source (const char *); void lexer_set_source (const char *);
@@ -159,4 +159,6 @@ void lexer_set_source (const char *);
token lexer_next_token (void); token lexer_next_token (void);
void lexer_save_token (token); void lexer_save_token (token);
void lexer_dump_buffer_state (void);
#endif #endif
+1 -1
View File
@@ -16,7 +16,7 @@
#ifndef MAPPINGS_H #ifndef MAPPINGS_H
#define MAPPINGS_H #define MAPPINGS_H
#ifndef JERRY_NDEBUG #ifndef __HOST
#include "../jerry-libc.h" #include "../jerry-libc.h"
#include "allocator.h" #include "allocator.h"
#include <stdarg.h> #include <stdarg.h>
+30 -16
View File
@@ -16,6 +16,18 @@
#include "parser.h" #include "parser.h"
#include "error.h" #include "error.h"
#include "lexer.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 bool
is_formal_parameter_list_empty (formal_parameter_list list) is_formal_parameter_list_empty (formal_parameter_list list)
@@ -68,7 +80,7 @@ is_statement_null (statement stmt)
static token tok; static token tok;
#ifdef JERRY_NDEBUG #ifdef __HOST
FILE *debug_file; FILE *debug_file;
#endif #endif
@@ -133,14 +145,14 @@ current_scope_must_be (unsigned int scopes)
static inline void static inline void
current_scope_must_be_global (void) current_scope_must_be_global (void)
{ {
if (scope_index != 0) if (scope_index != 1)
fatal (ERR_PARSER); fatal (ERR_PARSER);
} }
static void static void
push_scope (int type) push_scope (int type)
{ {
#ifdef JERRY_NDEBUG #ifdef __HOST
fprintf (debug_file, "push_scope: 0x%x\n", type); fprintf (debug_file, "push_scope: 0x%x\n", type);
#endif #endif
current_scopes[scope_index++] = (scope) { .type = type, .was_stmt = false }; current_scopes[scope_index++] = (scope) { .type = type, .was_stmt = false };
@@ -149,7 +161,7 @@ push_scope (int type)
static void static void
pop_scope (void) pop_scope (void)
{ {
#ifdef JERRY_NDEBUG #ifdef __HOST
fprintf (debug_file, "pop_scope: 0x%x\n", current_scopes[scope_index - 1].type); fprintf (debug_file, "pop_scope: 0x%x\n", current_scopes[scope_index - 1].type);
#endif #endif
scope_index--; scope_index--;
@@ -160,7 +172,7 @@ assert_keyword (keyword kw)
{ {
if (tok.type != TOK_KEYWORD || tok.data.kw != kw) if (tok.type != TOK_KEYWORD || tok.data.kw != kw)
{ {
#ifdef JERRY_NDEBUG #ifdef __HOST
printf ("assert_keyword: 0x%x\n", kw); printf ("assert_keyword: 0x%x\n", kw);
#endif #endif
JERRY_UNREACHABLE (); JERRY_UNREACHABLE ();
@@ -178,7 +190,7 @@ current_token_must_be(token_type tt)
{ {
if (tok.type != tt) if (tok.type != tt)
{ {
#ifdef JERRY_NDEBUG #ifdef __HOST
printf ("current_token_must_be: 0x%x\n", tt); printf ("current_token_must_be: 0x%x\n", tt);
#endif #endif
fatal (ERR_PARSER); fatal (ERR_PARSER);
@@ -199,7 +211,7 @@ next_token_must_be (token_type tt)
tok = lexer_next_token (); tok = lexer_next_token ();
if (tok.type != tt) if (tok.type != tt)
{ {
#ifdef JERRY_NDEBUG #ifdef __HOST
printf ("next_token_must_be: 0x%x\n", tt); printf ("next_token_must_be: 0x%x\n", tt);
#endif #endif
fatal (ERR_PARSER); fatal (ERR_PARSER);
@@ -786,7 +798,7 @@ parse_operator:
res.type = ET_PROP_REF; res.type = ET_PROP_REF;
skip_newlines (); skip_newlines ();
res.data.ops.op2 = parse_operand (); res.data.ops.op2 = parse_operand ();
assert (!res.data.ops.op2.is_literal); JERRY_ASSERT (!res.data.ops.op2.is_literal);
return res; return res;
case TOK_OPEN_SQUARE: case TOK_OPEN_SQUARE:
@@ -962,7 +974,7 @@ parse_for_or_for_in_statement (void)
fatal (ERR_PARSER); 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. */ /* expression contains left_hand_side_expression. */
expr = parse_expression (); expr = parse_expression ();
@@ -981,13 +993,13 @@ plain_for:
res.is_for_in = false; res.is_for_in = false;
if (!is_variable_declaration_empty(list.decls[0])) 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.is_decl = true;
res.data.for_stmt.init.data.decl_list = list; res.data.for_stmt.init.data.decl_list = list;
} }
if (!is_expression_empty(expr.exprs[0])) 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.is_decl = false;
res.data.for_stmt.init.data.expr = expr; res.data.for_stmt.init.data.expr = expr;
} }
@@ -1015,14 +1027,14 @@ for_in:
res.is_for_in = true; res.is_for_in = true;
if (!is_variable_declaration_empty(list.decls[0])) if (!is_variable_declaration_empty(list.decls[0]))
{ {
assert (is_expression_empty (expr.exprs[0])); JERRY_ASSERT (is_expression_empty (expr.exprs[0]));
assert (is_variable_declaration_empty (list.decls[1])); JERRY_ASSERT (is_variable_declaration_empty (list.decls[1]));
res.data.for_in_stmt.init.is_decl = true; res.data.for_in_stmt.init.is_decl = true;
res.data.for_in_stmt.init.data.decl = list.decls[0]; res.data.for_in_stmt.init.data.decl = list.decls[0];
} }
if (!is_expression_empty(expr.exprs[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.is_decl = false;
res.data.for_in_stmt.init.data.left_hand_expr = expr.exprs[0]; res.data.for_in_stmt.init.data.left_hand_expr = expr.exprs[0];
} }
@@ -1112,6 +1124,8 @@ parser_parse_statement (void)
statement res; statement res;
res.data.none = NULL; res.data.none = NULL;
JERRY_ASSERT (scope_index);
skip_newlines (); skip_newlines ();
if (is_keyword (KW_FINALLY)) if (is_keyword (KW_FINALLY))
@@ -1374,8 +1388,8 @@ parser_parse_statement (void)
void void
parser_init (void) parser_init (void)
{ {
scope_index = 0; scope_index = 1;
#ifdef JERRY_NDEBUG #ifdef __HOST
debug_file = fopen ("parser.log", "w"); debug_file = fopen ("parser.log", "w");
#endif #endif
} }
+38 -5
View File
@@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
#ifdef JERRY_NDEBUG #ifdef __HOST
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -39,6 +39,10 @@
#include "generated.h" #include "generated.h"
#include "lexer.h"
#include "parser.h"
#include "pretty-printer.h"
void fake_exit (void); void fake_exit (void);
void void
@@ -95,10 +99,11 @@ fake_exit (void)
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
statement st;
bool dump_tokens = false; bool dump_tokens = false;
bool dump_ast = true; bool dump_ast = false;
const char *file_name = NULL; const char *file_name = NULL;
#ifdef JERRY_NDEBUG #ifdef __HOST
FILE *file = NULL; FILE *file = NULL;
#endif #endif
@@ -124,16 +129,44 @@ main (int argc, char **argv)
if (dump_tokens && dump_ast) if (dump_tokens && dump_ast)
fatal (ERR_SEVERAL_FILES); fatal (ERR_SEVERAL_FILES);
#ifdef JERRY_NDEBUG if (!dump_tokens)
dump_ast = true;
#ifdef __HOST
file = fopen (file_name, "r"); file = fopen (file_name, "r");
if (file == NULL) if (file == NULL)
{ {
fatal (ERR_IO); fatal (ERR_IO);
} }
lexer_set_file (file);
#else
lexer_set_source (generated_source);
#endif #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 (generated_source);
//gen_bytecode (); //gen_bytecode ();
+1 -1
View File
@@ -548,7 +548,7 @@ dump_postfix (operand op, const char *operation)
static void static void
pp_assignment_expression (assignment_expression expr) pp_assignment_expression (assignment_expression expr)
{ {
if (expr.var) if (expr.oper != AO_NONE && expr.var)
printf ("%s", expr.var); printf ("%s", expr.var);
switch (expr.oper) switch (expr.oper)