From 7da78d76608586d72dfe117bc9d2bebfba41ac7d Mon Sep 17 00:00:00 2001 From: smaludzi Date: Wed, 29 Nov 2023 19:37:25 +0100 Subject: [PATCH] Let var temp constness (#63) Variable constness defined by let var keywords corrected. Also test_exec extended to support error conditions. --- back/nev.c | 2 + back/program.c | 12 ++ back/utils.c | 78 ++++++++++--- back/utils.h | 4 +- front/expr.c | 110 ++++++++---------- front/expr.h | 13 +-- front/param.c | 36 +++--- front/param.h | 4 +- front/parser.y | 12 ++ front/tcforin.c | 1 - front/tcheckarr.c | 14 ++- front/tcmatch.c | 99 +++++++++------- front/tcmatch.h | 1 + front/typecheck.c | 243 +++++++++++++++++++++------------------ front/typecheck.h | 14 +-- include/program.h | 3 + sample/sample198.nev | 7 +- sample/sample247.nev | 6 +- sample/sample281.nev | 2 +- sample/sample304.nev | 4 +- sample/sample350.nev | 2 +- sample/sample358.nev | 2 +- sample/sample360.nev | 8 +- sample/sample362.nev | 4 +- sample/sample443.nev | 2 +- sample/sample607.nev | 2 +- sample/sample609.nev | 4 +- sample/sample610.nev | 2 +- sample/sample615.nev | 2 +- sample/sample616.nev | 2 +- sample/sample619.nev | 2 +- sample/sample639.nev | 14 +++ sample/sample640.nev | 14 +++ sample/sample640.nev.err | 2 + sample/sample641.nev | 20 ++++ sample/sample642.nev | 16 +++ sample/sample643.nev | 40 +++++++ sample/sample644.nev | 23 ++++ sample/sample645.nev | 22 ++++ sample/sample646.nev | 21 ++++ sample/sample647.nev | 12 ++ sample/sample648.nev | 24 ++++ sample/sample649.nev | 13 +++ sample/sample650.nev | 15 +++ sample/sample651.nev | 15 +++ sample/sample652.nev | 15 +++ sample/sample653.nev | 15 +++ sample/sample653.nev.err | 2 + sample/sample654.nev | 17 +++ sample/sample655.nev | 17 +++ sample/sample655.nev.err | 2 + sample/sample656.nev | 17 +++ sample/sample656.nev.err | 4 + sample/sample657.nev | 16 +++ sample/sample658.nev | 16 +++ sample/sample658.nev.err | 2 + sample/sample659.nev | 16 +++ sample/sample659.nev.err | 1 + sample/sample660.nev | 17 +++ sample/sample660.nev.err | 1 + sample/sample661.nev | 21 ++++ sample/sample662.nev | 20 ++++ sample/sample663.nev | 20 ++++ sample/sample663.nev.err | 2 + sample/sample664.nev | 20 ++++ sample/sample665.nev | 20 ++++ sample/sample665.nev.err | 1 + sample/sample666.nev | 24 ++++ sample/sample666.nev.err | 1 + sample/sample667.nev | 12 ++ sample/sample668.nev | 12 ++ sample/sample668.nev.err | 1 + sample/sample669.nev | 11 ++ sample/sample670.nev | 11 ++ sample/sample670.nev.err | 1 + sample/sample671.nev | 10 ++ sample/sample671.nev.err | 2 + sample/sample672.nev | 10 ++ sample/sample672.nev.err | 1 + sample/sample673.nev | 14 +++ sample/sample673.nev.err | 2 + sample/sample674.nev | 15 +++ sample/sample674.nev.err | 2 + sample/sample675.nev | 14 +++ sample/sample675.nev.err | 2 + sample/sample676.nev | 7 ++ sample/sample676.nev.err | 2 + sample/sample677.nev | 11 ++ sample/sample677.nev.err | 1 + sample/sample678.nev | 11 ++ sample/sample679.nev | 10 ++ sample/sample679.nev.err | 1 + sample/sample680.nev | 10 ++ sample/sample680.nev.err | 1 + sample/sample681.nev | 10 ++ sample/sample681.nev.err | 1 + sample/sample682.nev | 10 ++ sample/sample683.nev | 9 ++ sample/sample683.nev.err | 1 + sample/sample684.nev | 9 ++ sample/sample684.nev.err | 1 + sample/sample685.nev | 10 ++ sample/sample686.nev | 19 +++ sample/sample686.nev.err | 2 + sample/sample687.nev | 16 +++ sample/sample687.nev.err | 1 + sample/sample688.nev | 16 +++ sample/sample688.nev.err | 2 + sample/sample689.nev | 25 ++++ sample/sample690.nev | 12 ++ sample/sample690.nev.err | 1 + sample/sample691.nev | 12 ++ sample/sample692.nev | 12 ++ sample/sample692.nev.err | 1 + sample/sample693.nev | 12 ++ sample/sample_qsort.nev | 2 +- sample/sample_wave.nev | 2 +- test/test_exec.c | 60 +++++++++- 118 files changed, 1349 insertions(+), 306 deletions(-) create mode 100644 sample/sample639.nev create mode 100644 sample/sample640.nev create mode 100644 sample/sample640.nev.err create mode 100644 sample/sample641.nev create mode 100644 sample/sample642.nev create mode 100644 sample/sample643.nev create mode 100644 sample/sample644.nev create mode 100644 sample/sample645.nev create mode 100644 sample/sample646.nev create mode 100644 sample/sample647.nev create mode 100644 sample/sample648.nev create mode 100644 sample/sample649.nev create mode 100644 sample/sample650.nev create mode 100644 sample/sample651.nev create mode 100644 sample/sample652.nev create mode 100644 sample/sample653.nev create mode 100644 sample/sample653.nev.err create mode 100644 sample/sample654.nev create mode 100644 sample/sample655.nev create mode 100644 sample/sample655.nev.err create mode 100644 sample/sample656.nev create mode 100644 sample/sample656.nev.err create mode 100644 sample/sample657.nev create mode 100644 sample/sample658.nev create mode 100644 sample/sample658.nev.err create mode 100644 sample/sample659.nev create mode 100644 sample/sample659.nev.err create mode 100644 sample/sample660.nev create mode 100644 sample/sample660.nev.err create mode 100644 sample/sample661.nev create mode 100644 sample/sample662.nev create mode 100644 sample/sample663.nev create mode 100644 sample/sample663.nev.err create mode 100644 sample/sample664.nev create mode 100644 sample/sample665.nev create mode 100644 sample/sample665.nev.err create mode 100644 sample/sample666.nev create mode 100644 sample/sample666.nev.err create mode 100644 sample/sample667.nev create mode 100644 sample/sample668.nev create mode 100644 sample/sample668.nev.err create mode 100644 sample/sample669.nev create mode 100644 sample/sample670.nev create mode 100644 sample/sample670.nev.err create mode 100644 sample/sample671.nev create mode 100644 sample/sample671.nev.err create mode 100644 sample/sample672.nev create mode 100644 sample/sample672.nev.err create mode 100644 sample/sample673.nev create mode 100644 sample/sample673.nev.err create mode 100644 sample/sample674.nev create mode 100644 sample/sample674.nev.err create mode 100644 sample/sample675.nev create mode 100644 sample/sample675.nev.err create mode 100644 sample/sample676.nev create mode 100644 sample/sample676.nev.err create mode 100644 sample/sample677.nev create mode 100644 sample/sample677.nev.err create mode 100644 sample/sample678.nev create mode 100644 sample/sample679.nev create mode 100644 sample/sample679.nev.err create mode 100644 sample/sample680.nev create mode 100644 sample/sample680.nev.err create mode 100644 sample/sample681.nev create mode 100644 sample/sample681.nev.err create mode 100644 sample/sample682.nev create mode 100644 sample/sample683.nev create mode 100644 sample/sample683.nev.err create mode 100644 sample/sample684.nev create mode 100644 sample/sample684.nev.err create mode 100644 sample/sample685.nev create mode 100644 sample/sample686.nev create mode 100644 sample/sample686.nev.err create mode 100644 sample/sample687.nev create mode 100644 sample/sample687.nev.err create mode 100644 sample/sample688.nev create mode 100644 sample/sample688.nev.err create mode 100644 sample/sample689.nev create mode 100644 sample/sample690.nev create mode 100644 sample/sample690.nev.err create mode 100644 sample/sample691.nev create mode 100644 sample/sample692.nev create mode 100644 sample/sample692.nev.err create mode 100644 sample/sample693.nev diff --git a/back/nev.c b/back/nev.c index 2c547c36..f5933968 100644 --- a/back/nev.c +++ b/back/nev.c @@ -124,6 +124,8 @@ static int nev_compile_prog(const char * input, program * prog) set_line_no(1); parse_result = 0; + set_msg_buffer(&prog->msg_count, &prog->msg_array_size, &prog->msg_array); + yyparse(&module_main); if ((ret = parse_result) == 0) { diff --git a/back/program.c b/back/program.c index 30e50bf9..337268b2 100644 --- a/back/program.c +++ b/back/program.c @@ -31,6 +31,9 @@ program * program_new() value->params = NULL; value->params_count = 0; value->entry_addr = 0; + value->msg_count = 0; + value->msg_array_size = 0; + value->msg_array = NULL; value->module_value = module_new(); return value; @@ -42,5 +45,14 @@ void program_delete(program * value) { module_delete(value->module_value); } + if (value->msg_array != NULL) + { + unsigned int i = 0; + for (i = 0; i < value->msg_count; i++) + { + free(value->msg_array[i]); + } + free(value->msg_array); + } free(value); } diff --git a/back/utils.c b/back/utils.c index 4930852d..76a49681 100644 --- a/back/utils.c +++ b/back/utils.c @@ -20,10 +20,17 @@ * THE SOFTWARE. */ #include "utils.h" +#include +#include #include #include -const char * utils_file_name = ""; +#define MAX_MSG_SIZE 1024 + +static const char * utils_file_name = ""; +static unsigned int * utils_msg_count = NULL; +static unsigned int * utils_msg_array_size = NULL; +static char *** utils_msg_array = NULL; const char * get_utils_file_name() { @@ -35,14 +42,34 @@ void set_utils_file_name(const char * file_name) utils_file_name = file_name; } -void print_error_msg(int line_no, const char * format, ...) +void set_msg_buffer(unsigned int * msg_count, unsigned int * msg_array_size, char *** msg_array) { - va_list args; + utils_msg_count = msg_count; + utils_msg_array_size = msg_array_size; + utils_msg_array = msg_array; +} - va_start(args, format); +void print_msg_buffer(unsigned int msg_count, char ** msg_array) +{ + if (msg_array == NULL) + { + return; + } + unsigned int i = 0; + for (i = 0; i < msg_count; i++) + { + printf("%s\n", msg_array[i]); + } +} - fprintf(stderr, "%s:%d: error: ", utils_file_name, line_no); - vfprintf(stderr, format, args); +static void print_msg(const char * type, int line_no, const char * format, va_list args) +{ + va_list args_copy; + + va_copy(args_copy, args); + fprintf(stderr, "%s:%d: %s: ", utils_file_name, line_no, type); + vfprintf(stderr, format, args_copy); + va_end(args_copy); #ifndef NO_FFI fprintf(stderr, "\n"); @@ -50,23 +77,44 @@ void print_error_msg(int line_no, const char * format, ...) fprintf(stderr, "\r\n"); #endif + if (utils_msg_count != NULL) + { + unsigned int msg_len = 0; + char msg_buf[MAX_MSG_SIZE] = { 0 }; + + if (*utils_msg_count >= *utils_msg_array_size) + { + *utils_msg_array_size = *utils_msg_array_size + 10; + *utils_msg_array = realloc(*utils_msg_array, *utils_msg_array_size * sizeof(char *)); + } + + va_copy(args_copy, args); + msg_len = snprintf(msg_buf, MAX_MSG_SIZE, "%s:%d: %s: ", utils_file_name, line_no, type); + msg_len += vsnprintf(msg_buf + msg_len, MAX_MSG_SIZE, format, args_copy); + va_end(args_copy); + + (*utils_msg_array)[*utils_msg_count] = strdup(msg_buf); + (*utils_msg_count)++; + } +} + +void print_error_msg(int line_no, const char * format, ...) +{ + va_list args; + va_start(args, format); + + print_msg("error", line_no, format, args); + va_end(args); } void print_warning_msg(int line_no, const char * format, ...) { va_list args; - va_start(args, format); - fprintf(stderr,"%s:%d: warning: ", utils_file_name, line_no); - vfprintf(stderr, format, args); - -#ifndef NO_FFI - fprintf(stderr, "\n"); -#else - fprintf(stderr, "\r\n"); -#endif + print_msg("warning", line_no, format, args); va_end(args); } + diff --git a/back/utils.h b/back/utils.h index 6e022a47..731298d4 100644 --- a/back/utils.h +++ b/back/utils.h @@ -25,8 +25,10 @@ const char * get_utils_file_name(); void set_utils_file_name(const char * file_name); -void print_error_msg(int line_no, const char * format, ...); +void set_msg_buffer(unsigned int * msg_count, unsigned int * msg_array_size, char *** msg_array); +void print_msg_buffer(unsigned int msg_count, char ** msg_array); +void print_error_msg(int line_no, const char * format, ...); void print_warning_msg(int line_no, const char * format, ...); #endif /* __UTILS_H__ */ diff --git a/front/expr.c b/front/expr.c index 8aacb096..a3a5a476 100644 --- a/front/expr.c +++ b/front/expr.c @@ -40,8 +40,7 @@ expr * expr_new_bool(int int_value) ret->type = EXPR_BOOL; ret->int_value = int_value; ret->comb.comb = COMB_TYPE_BOOL; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; return ret; @@ -54,8 +53,7 @@ expr * expr_new_int(int int_value) ret->type = EXPR_INT; ret->int_value = int_value; ret->comb.comb = COMB_TYPE_INT; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; return ret; @@ -68,8 +66,7 @@ expr * expr_new_long(long long long_value) ret->type = EXPR_LONG; ret->long_value = long_value; ret->comb.comb = COMB_TYPE_LONG; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; return ret; @@ -82,8 +79,7 @@ expr * expr_new_char(char char_value) ret->type = EXPR_CHAR; ret->char_value = char_value; ret->comb.comb = COMB_TYPE_CHAR; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; return ret; @@ -96,8 +92,7 @@ expr * expr_new_float(float float_value) ret->type = EXPR_FLOAT; ret->float_value = float_value; ret->comb.comb = COMB_TYPE_FLOAT; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; return ret; @@ -110,8 +105,7 @@ expr * expr_new_double(double double_value) ret->type = EXPR_DOUBLE; ret->double_value = double_value; ret->comb.comb = COMB_TYPE_DOUBLE; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; return ret; @@ -124,8 +118,7 @@ expr * expr_new_string(char * string_value) ret->type = EXPR_STRING; ret->string_value = string_value; ret->comb.comb = COMB_TYPE_STRING; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; return ret; @@ -142,8 +135,7 @@ expr * expr_new_enumtype(expr * enum_id, char * item_id) ret->enumtype.id_enumerator_value = NULL; ret->enumtype.id_enumtype_value = NULL; ret->comb.comb = COMB_TYPE_ENUMTYPE; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; return ret; @@ -156,8 +148,7 @@ expr * expr_new_id(char * id) ret->type = EXPR_ID; ret->id.id = id; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; return ret; @@ -169,8 +160,7 @@ expr * expr_new_nil() ret->type = EXPR_NIL; ret->comb.comb = COMB_TYPE_NIL; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; return ret; @@ -182,8 +172,7 @@ expr * expr_new_c_null() ret->type = EXPR_C_NULL; ret->comb.comb = COMB_TYPE_C_PTR; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; return ret; @@ -195,8 +184,7 @@ expr * expr_new_one(int type, expr * left) ret->type = type; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; ret->left = left; ret->middle = NULL; @@ -211,8 +199,7 @@ expr * expr_new_two(int type, expr * left, expr * right) ret->type = type; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; ret->left = left; ret->middle = NULL; @@ -227,8 +214,7 @@ expr * expr_new_three(int type, expr * left, expr * middle, expr * right) ret->type = type; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; ret->left = left; ret->middle = middle; @@ -243,8 +229,7 @@ expr * expr_new_array(array * value) ret->type = EXPR_ARRAY; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->comb.array.comb_dims = 0; ret->comb.array.comb_ret = NULL; ret->line_no = 0; @@ -259,8 +244,7 @@ expr * expr_new_array_deref(expr * array_expr, expr_list * ref) ret->type = EXPR_ARRAY_DEREF; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; ret->array_deref.array_expr = array_expr; ret->array_deref.ref = ref; @@ -274,8 +258,7 @@ expr * expr_new_range_dim(expr * from, expr * to) ret->type = EXPR_RANGE_DIM; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; ret->range_dim.from = from; ret->range_dim.to = to; @@ -289,8 +272,7 @@ expr * expr_new_range(expr_list * range_dims) ret->type = EXPR_RANGE; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; ret->range.ret = param_new_int(NULL); ret->range.ret->const_type = PARAM_CONST_TYPE_CONST; @@ -305,8 +287,7 @@ expr * expr_new_slice(expr * array_expr, expr_list * range_dims) ret->type = EXPR_SLICE; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; ret->slice.array_expr = array_expr; ret->slice.range_dims = range_dims; @@ -321,8 +302,7 @@ expr * expr_new_seq(seq * value) ret->type = EXPR_SEQ; ret->line_no = value != NULL ? value->line_no : 0; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->seq_value = value; return ret; @@ -335,8 +315,7 @@ expr * expr_new_func(func * value) ret->type = EXPR_FUNC; ret->line_no = 0; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->func_value = value; return ret; @@ -349,8 +328,7 @@ expr * expr_new_call(expr * func_expr, expr_list * params) ret->type = EXPR_CALL; ret->line_no = 0; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->call.func_expr = func_expr; ret->call.params = params; @@ -364,8 +342,7 @@ expr * expr_new_build_in(unsigned int id, expr_list * params, param * param_ret) ret->type = EXPR_BUILD_IN; ret->line_no = 0; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->func_build_in.id = id; ret->func_build_in.param = params; ret->func_build_in.ret = param_ret; @@ -380,8 +357,7 @@ expr * expr_new_ass(expr * left, expr * right) ret->type = EXPR_ASS; ret->line_no = 0; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->left = left; ret->right = right; @@ -395,8 +371,7 @@ expr * expr_new_while(expr * cond, expr * do_value) ret->type = EXPR_WHILE; ret->line_no = 0; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->whileloop.cond = cond; ret->whileloop.do_value = do_value; @@ -410,8 +385,7 @@ expr * expr_new_do_while(expr * cond, expr * do_value) ret->type = EXPR_DO_WHILE; ret->line_no = 0; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->whileloop.cond = cond; ret->whileloop.do_value = do_value; @@ -425,8 +399,7 @@ expr * expr_new_for(expr * init, expr * cond, expr * incr, expr * do_value) ret->type = EXPR_FOR; ret->line_no = 0; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->forloop.init = init; ret->forloop.cond = cond; ret->forloop.incr = incr; @@ -442,8 +415,7 @@ expr * expr_new_for_in(char * id, expr * in_value, expr * do_value) ret->type = EXPR_FORIN; ret->line_no = 0; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->forin_value = forin_new(id, in_value, do_value); return ret; @@ -456,8 +428,7 @@ expr * expr_new_iflet(iflet * iflet_value) ret->type = EXPR_IFLET; ret->line_no = 0; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->iflet_value = iflet_value; return ret; @@ -470,8 +441,7 @@ expr * expr_new_match(expr * expr_value, match_guard_list * match_guards) ret->type = EXPR_MATCH; ret->line_no = 0; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->match.expr_value = expr_value; ret->match.match_guards = match_guards; @@ -485,8 +455,7 @@ expr * expr_new_listcomp(listcomp * listcomp_value) ret->type = EXPR_LISTCOMP; ret->line_no = 0; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->listcomp_value = listcomp_value; return ret; @@ -499,8 +468,7 @@ expr * expr_new_attr(expr * record_value, expr * id) ret->type = EXPR_ATTR; ret->line_no = 0; ret->comb.comb = COMB_TYPE_UNKNOWN; - ret->comb.comb_const = COMB_CONST_TYPE_CONST; - ret->comb.comb_lr = COMB_LR_TYPE_RIGHT; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->attr.record_value = record_value; ret->attr.id = id; @@ -513,6 +481,8 @@ expr * expr_new_touple(touple * touple_value) ret->type = EXPR_TOUPLE; ret->touple_value = touple_value; + ret->comb.comb = COMB_TYPE_UNKNOWN; + ret->comb.comb_const = COMB_CONST_TYPE_TEMP; ret->line_no = 0; return ret; @@ -1044,6 +1014,18 @@ const char * comb_type_str(comb_type type) return "unknown comb type!"; } +const char * comb_const_type_str(comb_const_type type) +{ + switch (type) + { + case COMB_CONST_TYPE_TEMP: return "temp"; + case COMB_CONST_TYPE_CONST: return "const"; + case COMB_CONST_TYPE_VAR: return "var"; + } + + return "unknown comb const type!"; +} + const char * conv_type_str(conv_type conv) { switch (conv) diff --git a/front/expr.h b/front/expr.h index c0f3062f..d1ca5ad0 100644 --- a/front/expr.h +++ b/front/expr.h @@ -134,18 +134,11 @@ typedef enum comb_type typedef enum comb_const_type { - COMB_CONST_TYPE_UNKNOWN = 0, COMB_CONST_TYPE_CONST, - COMB_CONST_TYPE_VAR + COMB_CONST_TYPE_VAR, + COMB_CONST_TYPE_TEMP } comb_const_type; -typedef enum comb_lr_type -{ - COMB_LR_TYPE_UNKNOWN = 0, - COMB_LR_TYPE_LEFT = 1, - COMB_LR_TYPE_RIGHT = 2 -} comb_lr_type; - typedef enum id_type { ID_TYPE_UNKNOWN = 0, @@ -187,7 +180,6 @@ typedef struct expr_comb { comb_type comb; comb_const_type comb_const; /* constness */ - comb_lr_type comb_lr; /* lr type */ union { struct @@ -410,6 +402,7 @@ expr * expr_list_get_first(expr_list * list); const char * expr_type_str(expr_type type); const char * comb_type_str(comb_type type); +const char * comb_const_type_str(comb_const_type type); const char * conv_type_str(conv_type conv); #endif /* __EXPR_H__ */ diff --git a/front/param.c b/front/param.c index 5029f28f..e4261f8e 100644 --- a/front/param.c +++ b/front/param.c @@ -32,7 +32,7 @@ param * param_new_bool(char * id) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_BOOL; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->line_no = 0; @@ -45,7 +45,7 @@ param * param_new_int(char * id) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_INT; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->line_no = 0; @@ -58,7 +58,7 @@ param * param_new_long(char * id) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_LONG; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->line_no = 0; @@ -71,7 +71,7 @@ param * param_new_float(char * id) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_FLOAT; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->line_no = 0; @@ -84,7 +84,7 @@ param * param_new_double(char * id) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_DOUBLE; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->line_no = 0; @@ -97,7 +97,7 @@ param * param_new_char(char * id) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_CHAR; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->line_no = 0; @@ -110,7 +110,7 @@ param * param_new_string(char * id) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_STRING; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->line_no = 0; @@ -123,7 +123,7 @@ param * param_new_void(char * id) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_VOID; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = NULL; value->line_no = 0; @@ -136,7 +136,7 @@ param * param_new_c_ptr(char * id) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_C_PTR; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->line_no = 0; @@ -149,7 +149,7 @@ param * param_new_dim(char * id) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_DIM; - value->const_type = PARAM_CONST_TYPE_CONST; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->array_dim.array = NULL; @@ -164,7 +164,7 @@ param * param_new_array(char * id, param_list * dims, param * ret) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_ARRAY; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->array.dims = dims; @@ -184,7 +184,7 @@ param * param_new_range(char * id, range_list * ranges) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_RANGE; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->range.ranges = ranges; @@ -205,7 +205,7 @@ param * param_new_range_dim(char * id) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_RANGE_DIM; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_VAR; value->index = -1; value->id = id; value->line_no = 0; @@ -218,7 +218,7 @@ param * param_new_slice(char * id, range_list * ranges, param * ret) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_SLICE; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->slice.ranges = ranges; @@ -238,7 +238,7 @@ param * param_new_record(char * id, char * record_id) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_RECORD; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->record.record_id = record_id; @@ -254,7 +254,7 @@ param * param_new_record_module(char * id, char * record_id, char * module_id) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_RECORD; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->record.record_id = record_id; @@ -270,7 +270,7 @@ param * param_new_func(char * id, param_list * params, param * ret) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_FUNC; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->index = -1; value->id = id; value->func.params = params; @@ -285,7 +285,7 @@ param * param_new_touple(char * id, param_list * dims) param * value = (param *)malloc(sizeof(param)); value->type = PARAM_TOUPLE; - value->const_type = PARAM_CONST_TYPE_UNKNOWN; + value->const_type = PARAM_CONST_TYPE_DEFAULT; value->id = id; value->touple.dims = dims; value->line_no = 0; diff --git a/front/param.h b/front/param.h index 6c34ac0e..7d406345 100644 --- a/front/param.h +++ b/front/param.h @@ -55,9 +55,9 @@ typedef enum param_type typedef enum param_const_type { - PARAM_CONST_TYPE_UNKNOWN = 0, + PARAM_CONST_TYPE_DEFAULT, PARAM_CONST_TYPE_CONST, - PARAM_CONST_TYPE_VAR + PARAM_CONST_TYPE_VAR, } param_const_type; typedef struct param diff --git a/front/parser.y b/front/parser.y index 0940ecbd..97fd95e3 100644 --- a/front/parser.y +++ b/front/parser.y @@ -670,12 +670,24 @@ match_guard_item: TOK_ID TOK_DOT TOK_ID TOK_DDOT TOK_ID $$->line_no = $1; }; +match_guard_record: TOK_ID TOK_DDOT TOK_ID '(' ')' +{ + $$ = match_guard_record_new($1, $3, NULL); + $$->line_no = $1; +}; + match_guard_record: TOK_ID TOK_DDOT TOK_ID '(' matchbind_list ')' { $$ = match_guard_record_new($1, $3, $5); $$->line_no = $1; }; +match_guard_record: TOK_ID TOK_DOT TOK_ID TOK_DDOT TOK_ID '(' ')' +{ + $$ = match_guard_record_new_module($3, $5, $1, NULL); + $$->line_no = $1; +}; + match_guard_record: TOK_ID TOK_DOT TOK_ID TOK_DDOT TOK_ID '(' matchbind_list ')' { $$ = match_guard_record_new_module($3, $5, $1, $7); diff --git a/front/tcforin.c b/front/tcforin.c index 01b8e76a..3dbfdb4d 100644 --- a/front/tcforin.c +++ b/front/tcforin.c @@ -38,7 +38,6 @@ int symtab_add_param_from_forin(symtab * tab, forin * value, expr_set_comb_type(value->iter, value->in_value->comb.array.comb_ret); value->iter->comb.comb_const = value->in_value->comb.comb_const; - value->iter->comb.comb_lr = COMB_LR_TYPE_LEFT; symtab_add_forin(tab, value, syn_level); break; diff --git a/front/tcheckarr.c b/front/tcheckarr.c index dc6be744..9b4ae5d4 100644 --- a/front/tcheckarr.c +++ b/front/tcheckarr.c @@ -204,7 +204,11 @@ int array_check_type(symtab * tab, expr * value, func * func_value, unsigned int if (value->array.array_value->ret != NULL) { - param_check_type(tab, value->array.array_value->ret, syn_level, false, PARAM_CONST_TYPE_VAR, result); + if (value->array.array_value->ret->const_type == PARAM_CONST_TYPE_DEFAULT) + { + value->array.array_value->ret->const_type = PARAM_CONST_TYPE_VAR; + } + param_check_type(tab, value->array.array_value->ret, syn_level, result); } array_well_formed(value, &arr_result); @@ -212,7 +216,6 @@ int array_check_type(symtab * tab, expr * value, func * func_value, unsigned int { value->comb.comb = COMB_TYPE_ARRAY; value->comb.comb_const = value->array.array_value->ret->const_type; - value->comb.comb_lr = COMB_LR_TYPE_RIGHT; value->comb.array.comb_ret = value->array.array_value->ret; value->comb.array.comb_dims = value->array.array_value->dims->count; } @@ -251,13 +254,16 @@ int array_check_type(symtab * tab, expr * value, func * func_value, unsigned int param_type_str(value->array.array_value->ret->type)); } - param_check_type(tab, value->array.array_value->ret, syn_level, false, PARAM_CONST_TYPE_VAR, ¶m_result); + if (value->array.array_value->ret->const_type == PARAM_CONST_TYPE_DEFAULT) + { + value->array.array_value->ret->const_type = PARAM_CONST_TYPE_VAR; + } + param_check_type(tab, value->array.array_value->ret, syn_level, ¶m_result); if (param_result == TYPECHECK_SUCC) { value->comb.comb = COMB_TYPE_ARRAY; value->comb.comb_const = value->array.array_value->ret->const_type; - value->comb.comb_lr = COMB_LR_TYPE_RIGHT; value->comb.array.comb_ret = value->array.array_value->ret; value->comb.array.comb_dims = value->array.array_value->dims->count; } diff --git a/front/tcmatch.c b/front/tcmatch.c index 4e690d19..c43becc6 100644 --- a/front/tcmatch.c +++ b/front/tcmatch.c @@ -52,46 +52,14 @@ int symtab_add_matchbind_from_matchbind(symtab * tab, matchbind * matchbind_valu int symtab_add_matchbind_from_matchbind_list(symtab * tab, match_guard_record * guard_record, unsigned int syn_level, int * result) { - matchbind_list * matchbinds = guard_record->matchbinds; - enumtype * enumtype_value = guard_record->enumtype_value; - enumerator * enumerator_value = guard_record->enumerator_value; - if (guard_record->stab == NULL) { guard_record->stab = symtab_new(8, SYMTAB_TYPE_BLOCK, tab); } - if (matchbinds == NULL || enumtype_value == NULL || enumerator_value == NULL) - { - return 0; - } - - if ((enumerator_value->record_value == NULL || - enumerator_value->record_value->params == NULL) && matchbinds->count == 0) - { - return 0; - } - else if ((enumerator_value->record_value == NULL || - enumerator_value->record_value->params == NULL) && matchbinds->count > 0) - { - *result = TYPECHECK_FAIL; - print_error_msg(guard_record->line_no, "enum record %s.%s takes no params while guard has %d", - guard_record->enum_id, - guard_record->item_id, - matchbinds->count); - return 0; - } - - if (enumerator_value->record_value->params->count != matchbinds->count) - { - *result = TYPECHECK_FAIL; - print_error_msg(guard_record->line_no, "enum record %s.%s takes %d params while guard has %d", - guard_record->enum_id, - guard_record->item_id, - enumerator_value->record_value->params->count, - matchbinds->count); - return 0; - } + matchbind_list * matchbinds = guard_record->matchbinds; + enumtype * enumtype_value = guard_record->enumtype_value; + enumerator * enumerator_value = guard_record->enumerator_value; matchbind_list_node * matchbind_node = matchbinds->tail; param_list_node * param_node = enumerator_value->record_value->params->tail; @@ -152,7 +120,7 @@ int expr_match_guard_item_check_type(symtab * tab, match_guard_item * guard_item else { *result = TYPECHECK_FAIL; - print_error_msg(guard_item->line_no, "cannot find enum item %s.%s", + print_error_msg(guard_item->line_no, "cannot find enum item %s::%s", guard_item->enum_id, guard_item->item_id); } } @@ -206,6 +174,8 @@ int expr_match_guard_record_check_type(symtab * tab, match_guard_record * guard_ { guard_record->enumtype_value = entry->enumtype_value; guard_record->enumerator_value = enumerator_value; + + expr_match_guard_record_check_type_n(guard_record, result); } else { @@ -230,7 +200,55 @@ int expr_match_guard_record_check_type(symtab * tab, match_guard_record * guard_ } return 0; -} +} + +int expr_match_guard_record_check_type_n(match_guard_record * guard_record, int * result) +{ + matchbind_list * matchbinds = guard_record->matchbinds; + enumerator * enumerator_value = guard_record->enumerator_value; + + if ((enumerator_value->record_value == NULL || + enumerator_value->record_value->params == NULL) && + (matchbinds == NULL || + matchbinds->count == 0)) + { + return 0; + } + else if ((enumerator_value->record_value == NULL || + enumerator_value->record_value->params == NULL) && matchbinds->count > 0) + { + *result = TYPECHECK_FAIL; + print_error_msg(guard_record->line_no, "enum record %s::%s takes no params while guard has %d", + guard_record->enum_id, + guard_record->item_id, + matchbinds->count); + return 0; + } + + if (enumerator_value->record_value->params != NULL && + matchbinds == NULL) + { + *result = TYPECHECK_FAIL; + print_error_msg(guard_record->line_no, "enum record %s::%s takes %d params while guard has %d", + guard_record->enum_id, + guard_record->item_id, + enumerator_value->record_value->params->count, 0); + return 0; + } + + if (enumerator_value->record_value->params->count != matchbinds->count) + { + *result = TYPECHECK_FAIL; + print_error_msg(guard_record->line_no, "enum record %s::%s takes %d params while guard has %d", + guard_record->enum_id, + guard_record->item_id, + enumerator_value->record_value->params->count, + matchbinds->count); + return 0; + } + + return 0; +} int expr_match_guard_check_type(symtab * tab, match_guard * match_value, func * func_value, unsigned int syn_level, @@ -386,7 +404,7 @@ int expr_match_guard_mark_item(match_guard * match_value) if (enumerator_value->mark == 1) { print_warning_msg(match_value->line_no, - "repeated enum name %s.%s in match expression", + "repeated enum name %s::%s in match expression", match_value->guard_item.guard->enum_id, match_value->guard_item.guard->item_id); } @@ -474,7 +492,7 @@ int expr_match_guard_are_all_mark_items(expr * value, int * result) if (enumerator_value != NULL && enumerator_value->mark == 0) { print_error_msg(value->line_no, - "match expression does not cover %s.%s enum", + "match expression does not cover %s::%s enum", enum_value->id, enumerator_value->id); *result = TYPECHECK_FAIL; } @@ -581,7 +599,7 @@ int expr_match_check_type(symtab * tab, expr * value, func * func_value, /* initially expression type is of check expression */ value->comb = value->match.expr_value->comb; - + if (value->match.match_guards != NULL) { expr_match_guard_list_check_type(tab, value->match.match_guards, func_value, syn_level, result); @@ -596,6 +614,7 @@ int expr_match_check_type(symtab * tab, expr * value, func * func_value, expr_match_guard_list_right_cmp(value, value->match.match_guards, result); } + value->comb.comb_const = COMB_CONST_TYPE_TEMP; if (*result == TYPECHECK_FAIL) { value->comb.comb = COMB_TYPE_ERR; diff --git a/front/tcmatch.h b/front/tcmatch.h index 076739ad..b2bbb27c 100644 --- a/front/tcmatch.h +++ b/front/tcmatch.h @@ -41,6 +41,7 @@ int expr_match_guard_item_check_type(symtab * tab, match_guard_item * guard_item int * result); int expr_match_guard_record_check_type(symtab * tab, match_guard_record * guard_item, int * result); +int expr_match_guard_record_check_type_n(match_guard_record * guard_item, int * result); int expr_match_guard_check_type(symtab * tab, match_guard * match_value, func * func_value, unsigned int syn_level, int * result); int expr_match_guard_list_check_type(symtab * tab, match_guard_list * list, diff --git a/front/typecheck.c b/front/typecheck.c index df70f664..45130294 100644 --- a/front/typecheck.c +++ b/front/typecheck.c @@ -41,11 +41,30 @@ #include #include + +int param_list_set_default_var(param_list * params) +{ + param_list_node * node = NULL; + + node = params->tail; + while (node != NULL) + { + param * value = node->value; + if (value != NULL && + value->const_type == PARAM_CONST_TYPE_DEFAULT) + { + value->const_type = PARAM_CONST_TYPE_VAR; + } + node = node->next; + } + return 0; +} + int record_enum_param_list(param_list * params) { int index = 0; param_list_node * node = NULL; - + node = params->tail; while (node != NULL) { @@ -103,20 +122,7 @@ int expr_set_comb_type(expr * value, param * param_value) return 0; } - switch (param_value->const_type) - { - case PARAM_CONST_TYPE_CONST: - value->comb.comb_const = COMB_CONST_TYPE_CONST; - value->comb.comb_lr = COMB_LR_TYPE_LEFT; - break; - case PARAM_CONST_TYPE_VAR: - value->comb.comb_const = COMB_CONST_TYPE_VAR; - value->comb.comb_lr = COMB_LR_TYPE_LEFT; - break; - case PARAM_CONST_TYPE_UNKNOWN: - assert(0); - break; - } + param_expr_cmp_init(param_value->const_type, value); switch (param_value->type) { @@ -191,6 +197,25 @@ int expr_set_comb_type(expr * value, param * param_value) return 0; } +int expr_bind_set_comb_type(expr * value, bind * bind_value) +{ + value->comb = bind_value->expr_value->comb; + switch (bind_value->type) + { + case BIND_UNKNOWN: + assert(0); + break; + case BIND_LET: + value->comb.comb_const = COMB_CONST_TYPE_CONST; + break; + case BIND_VAR: + value->comb.comb_const = COMB_CONST_TYPE_VAR; + break; + } + + return 0; +} + int expr_set_comb_type_symtab(expr * value, symtab_entry * entry, unsigned int syn_level, int * result) { switch (entry->type) @@ -201,7 +226,6 @@ int expr_set_comb_type_symtab(expr * value, symtab_entry * entry, unsigned int s func * func_value = entry->func_value; value->comb.comb = COMB_TYPE_FUNC; - value->comb.comb_lr = COMB_LR_TYPE_RIGHT; value->comb.func.comb_params = func_value->decl->params; value->comb.func.comb_ret = func_value->decl->ret; } @@ -215,7 +239,7 @@ int expr_set_comb_type_symtab(expr * value, symtab_entry * entry, unsigned int s case SYMTAB_BIND: if (entry->bind_value != NULL) { - value->comb = entry->bind_value->expr_value->comb; + expr_bind_set_comb_type(value, entry->bind_value); } break; case SYMTAB_MATCHBIND: @@ -240,7 +264,6 @@ int expr_set_comb_type_symtab(expr * value, symtab_entry * entry, unsigned int s if (entry->record_value != NULL) { value->comb.comb = COMB_TYPE_RECORD_ID; - value->comb.comb_lr = COMB_LR_TYPE_RIGHT; value->comb.comb_record = entry->record_value; } break; @@ -249,7 +272,6 @@ int expr_set_comb_type_symtab(expr * value, symtab_entry * entry, unsigned int s { enumtype * al_enumtype = entry->enumtype_value; value->comb.comb = COMB_TYPE_ENUMTYPE_ID; - value->comb.comb_lr = COMB_LR_TYPE_RIGHT; value->comb.comb_enumtype = al_enumtype; } break; @@ -268,7 +290,6 @@ int expr_set_comb_type_symtab(expr * value, symtab_entry * entry, unsigned int s if (entry->module_decl_value && entry->module_decl_value->is_checked == 1) { value->comb.comb = COMB_TYPE_MODULE; - value->comb.comb_lr = COMB_LR_TYPE_RIGHT; value->comb.comb_module_decl = entry->module_decl_value; } else @@ -301,7 +322,6 @@ int expr_qualifier_set_comb_type(expr * value, expr * expr_value, int * result) expr_value->comb.range.comb_dims == 1) { value->comb.comb = COMB_TYPE_INT; - value->comb.comb_lr = COMB_LR_TYPE_RIGHT; } else { @@ -823,13 +843,15 @@ int param_expr_cmp(param * param_value, expr * expr_value, bool const_cmp) if (param_value->id != NULL) { print_error_msg(expr_value->line_no, - "passing const expression to variable param %s at line %u", + "passing %s expression to variable param %s at line %u", + comb_const_type_str(expr_value->comb.comb_const), param_value->id, param_value->line_no); } else { print_error_msg(expr_value->line_no, - "passing const expression to variable param at line %u", + "passing %s expression to variable param at line %u", + comb_const_type_str(expr_value->comb.comb_const), param_value->line_no); } return TYPECHECK_FAIL; @@ -1207,16 +1229,14 @@ int param_expr_cmp_init(param_const_type param_const, expr * expr_value) { switch (param_const) { + case PARAM_CONST_TYPE_DEFAULT: + expr_value->comb.comb_const = COMB_CONST_TYPE_CONST; + break; case PARAM_CONST_TYPE_CONST: expr_value->comb.comb_const = COMB_CONST_TYPE_CONST; - expr_value->comb.comb_lr = COMB_LR_TYPE_LEFT; break; case PARAM_CONST_TYPE_VAR: expr_value->comb.comb_const = COMB_CONST_TYPE_VAR; - expr_value->comb.comb_lr = COMB_LR_TYPE_LEFT; - break; - case PARAM_CONST_TYPE_UNKNOWN: - assert(0); break; } @@ -1677,27 +1697,22 @@ int param_enum_record_check_type(symtab * tab, param * param_value, int param_range_check_type(symtab * tab, range * value, unsigned int syn_level, - bool change_const_allowed, - param_const_type const_type, int * result) { if (value->from) { - param_check_type(tab, value->from, syn_level, change_const_allowed, const_type, result); + param_check_type(tab, value->from, syn_level, result); } if (value->to) { - param_check_type(tab, value->to, syn_level, change_const_allowed, const_type, result); + param_check_type(tab, value->to, syn_level, result); } return 0; } int param_range_list_check_type(symtab * tab, range_list * list, - unsigned int syn_level, - bool change_const_allowed, - param_const_type const_type, - int * result) + unsigned int syn_level, int * result) { range_list_node * node = list->tail; while (node != NULL) @@ -1705,7 +1720,7 @@ int param_range_list_check_type(symtab * tab, range_list * list, range * range_value = node->value; if (range_value != NULL) { - param_range_check_type(tab, range_value, syn_level, change_const_allowed, const_type, result); + param_range_check_type(tab, range_value, syn_level, result); } node = node->next; } @@ -1716,11 +1731,6 @@ int param_range_list_check_type(symtab * tab, range_list * list, int param_ffi_check_type(symtab * tab, param * param_value, unsigned int syn_level, int * result) { - if (param_value->const_type == PARAM_CONST_TYPE_UNKNOWN) - { - param_value->const_type = PARAM_CONST_TYPE_CONST; - } - switch (param_value->type) { case PARAM_BOOL: @@ -1757,29 +1767,11 @@ int param_ffi_check_type(symtab * tab, param * param_value, } int param_check_type(symtab * tab, param * param_value, - unsigned int syn_level, - bool change_const_allowed, - param_const_type const_type, int * result) + unsigned int syn_level, int * result) { - if (change_const_allowed == false && - param_value->const_type != PARAM_CONST_TYPE_UNKNOWN && - param_value->const_type != const_type) - { - *result = TYPECHECK_FAIL; - if (param_value->id != NULL) - { - print_error_msg(param_value->line_no, "cannot change param %s constness", - param_value->id); - } - else - { - print_error_msg(param_value->line_no, "cannot change param constness"); - } - } - - if (param_value->const_type == PARAM_CONST_TYPE_UNKNOWN) + if (param_value->const_type == PARAM_CONST_TYPE_DEFAULT) { - param_value->const_type = const_type; + param_value->const_type = PARAM_CONST_TYPE_CONST; } switch (param_value->type) @@ -1805,21 +1797,25 @@ int param_check_type(symtab * tab, param * param_value, case PARAM_ARRAY: if (param_value->array.dims != NULL) { - param_list_check_type(tab, param_value->array.dims, syn_level, false, PARAM_CONST_TYPE_CONST, result); + param_list_check_type(tab, param_value->array.dims, syn_level, result); } if (param_value->array.ret != NULL) { - param_check_type(tab, param_value->array.ret, syn_level, false, PARAM_CONST_TYPE_VAR, result); + if (param_value->array.ret->const_type == PARAM_CONST_TYPE_DEFAULT) + { + param_value->array.ret->const_type = PARAM_CONST_TYPE_VAR; + } + param_check_type(tab, param_value->array.ret, syn_level, result); } break; case PARAM_RANGE: if (param_value->range.ranges != NULL) { - param_range_list_check_type(tab, param_value->range.ranges, syn_level, change_const_allowed, param_value->const_type, result); + param_range_list_check_type(tab, param_value->range.ranges, syn_level, result); } if (param_value->range.ret != NULL) { - param_check_type(tab, param_value->range.ret, syn_level, change_const_allowed, param_value->const_type, result); + param_check_type(tab, param_value->range.ret, syn_level, result); } break; case PARAM_RANGE_DIM: @@ -1828,11 +1824,11 @@ int param_check_type(symtab * tab, param * param_value, case PARAM_SLICE: if (param_value->slice.ranges != NULL) { - param_range_list_check_type(tab, param_value->slice.ranges, syn_level, false, PARAM_CONST_TYPE_CONST, result); + param_range_list_check_type(tab, param_value->slice.ranges, syn_level, result); } if (param_value->slice.ret != NULL) { - param_check_type(tab, param_value->slice.ret, syn_level, false, PARAM_CONST_TYPE_VAR, result); + param_check_type(tab, param_value->slice.ret, syn_level, result); } break; case PARAM_RECORD: @@ -1841,17 +1837,17 @@ int param_check_type(symtab * tab, param * param_value, case PARAM_FUNC: if (param_value->func.params != NULL) { - param_list_check_type(tab, param_value->func.params, syn_level, true, PARAM_CONST_TYPE_CONST, result); + param_list_check_type(tab, param_value->func.params, syn_level, result); } if (param_value->func.ret != NULL) { - param_check_type(tab, param_value->func.ret, syn_level, false, PARAM_CONST_TYPE_VAR, result); + param_check_type(tab, param_value->func.ret, syn_level, result); } break; case PARAM_TOUPLE: if (param_value->touple.dims != NULL) { - param_list_check_type(tab, param_value->touple.dims, syn_level, false, PARAM_CONST_TYPE_VAR, result); + param_list_check_type(tab, param_value->touple.dims, syn_level, result); } break; } @@ -1876,9 +1872,7 @@ int param_list_ffi_check_type(symtab * tab, param_list * list, } int param_list_check_type(symtab * tab, param_list * list, - unsigned int syn_level, - bool change_const_allowed, - param_const_type const_type, int * result) + unsigned int syn_level, int * result) { param_list_node * node = list->tail; while (node != NULL) @@ -1886,7 +1880,7 @@ int param_list_check_type(symtab * tab, param_list * list, param * param_value = node->value; if (param_value != NULL) { - param_check_type(tab, param_value, syn_level, change_const_allowed, const_type, result); + param_check_type(tab, param_value, syn_level, result); } node = node->next; } @@ -1956,6 +1950,9 @@ int expr_neg_check_type(symtab * tab, expr * value, func * func_value, unsigned int syn_level, int * result) { expr_check_type(tab, value->left, func_value, syn_level, result); + + value->comb.comb_const = COMB_CONST_TYPE_TEMP; + if (value->left->comb.comb == COMB_TYPE_INT) { value->comb.comb = COMB_TYPE_INT; @@ -2022,7 +2019,7 @@ int expr_add_sub_check_type(symtab * tab, expr * value, func * func_value, expr_check_type(tab, value->left, func_value, syn_level, result); expr_check_type(tab, value->right, func_value, syn_level, result); - value->comb.comb_const = COMB_CONST_TYPE_CONST; + value->comb.comb_const = COMB_CONST_TYPE_TEMP; if (expr_conv_basic_type(value, value->left, value->right)) { @@ -2095,6 +2092,9 @@ int expr_mul_check_type(symtab * tab, expr * value, func * func_value, unsigned { expr_check_type(tab, value->left, func_value, syn_level, result); expr_check_type(tab, value->right, func_value, syn_level, result); + + value->comb.comb_const = COMB_CONST_TYPE_TEMP; + if (expr_conv_basic_type(value, value->left, value->right)) { /* conversion done */ @@ -2341,6 +2341,9 @@ int expr_div_check_type(symtab * tab, expr * value, func * func_value, unsigned { expr_check_type(tab, value->left, func_value, syn_level, result); expr_check_type(tab, value->right, func_value, syn_level, result); + + value->comb.comb_const = COMB_CONST_TYPE_TEMP; + if (expr_conv_basic_type(value, value->left, value->right)) { /* conversion done */ @@ -2366,6 +2369,9 @@ int expr_mod_check_type(symtab * tab, expr * value, func * func_value, unsigned { expr_check_type(tab, value->left, func_value, syn_level, result); expr_check_type(tab, value->right, func_value, syn_level, result); + + value->comb.comb_const = COMB_CONST_TYPE_TEMP; + if (value->left->comb.comb == COMB_TYPE_INT && value->right->comb.comb == COMB_TYPE_INT) { @@ -2415,6 +2421,8 @@ int expr_lgte_check_type(symtab * tab, expr * value, func * func_value, unsigned expr_check_type(tab, value->left, func_value, syn_level, result); expr_check_type(tab, value->right, func_value, syn_level, result); + value->comb.comb_const = COMB_CONST_TYPE_TEMP; + if (expr_conv_basic_type(value, value->left, value->right)) { /* converted type */ @@ -2446,6 +2454,9 @@ int expr_and_or_check_type(symtab * tab, expr * value, func * func_value, unsign { expr_check_type(tab, value->left, func_value, syn_level, result); expr_check_type(tab, value->right, func_value, syn_level, result); + + value->comb.comb_const = COMB_CONST_TYPE_TEMP; + if (value->left->comb.comb == COMB_TYPE_BOOL && value->right->comb.comb == COMB_TYPE_BOOL) { @@ -2470,6 +2481,8 @@ int expr_complr_check_type(symtab * tab, expr * value, func * func_value, unsign expr_check_type(tab, value_left, func_value, syn_level, result); + value->comb.comb_const = COMB_CONST_TYPE_TEMP; + if (value_right->type != EXPR_CALL && value_right->type != EXPR_LAST_CALL) { @@ -2535,6 +2548,9 @@ int expr_complr_check_type(symtab * tab, expr * value, func * func_value, unsign int expr_not_check_type(symtab * tab, expr * value, func * func_value, unsigned int syn_level, int * result) { expr_check_type(tab, value->left, func_value, syn_level, result); + + value->comb.comb_const = COMB_CONST_TYPE_TEMP; + if (value->left->comb.comb == COMB_TYPE_BOOL) { value->comb.comb = COMB_TYPE_BOOL; @@ -2553,6 +2569,9 @@ int expr_not_check_type(symtab * tab, expr * value, func * func_value, unsigned int expr_bin_not_check_type(symtab * tab, expr * value, func * func_value, unsigned int syn_level, int * result) { expr_check_type(tab, value->left, func_value, syn_level, result); + + value->comb.comb_const = COMB_CONST_TYPE_TEMP; + if (value->left->comb.comb == COMB_TYPE_INT) { value->comb.comb = COMB_TYPE_INT; @@ -2582,6 +2601,8 @@ int expr_bin_op_check_type(symtab * tab, expr * value, func * func_value, unsign expr_check_type(tab, value->left, func_value, syn_level, result); expr_check_type(tab, value->right, func_value, syn_level, result); + value->comb.comb_const = COMB_CONST_TYPE_TEMP; + if (value->left->comb.comb == COMB_TYPE_INT && value->right->comb.comb == COMB_TYPE_INT) { @@ -2629,6 +2650,8 @@ int expr_ass_check_type(symtab * tab, expr * value, func * func_value, unsigned if (value->left->type != EXPR_ID && value->left->type != EXPR_ATTR && + value->left->type != EXPR_CALL && + value->left->type != EXPR_LAST_CALL && value->left->type != EXPR_ARRAY_DEREF) { print_warning_msg(value->line_no, @@ -2636,16 +2659,18 @@ int expr_ass_check_type(symtab * tab, expr * value, func * func_value, unsigned expr_type_str(value->left->type)); } - if (value->left->comb.comb_const != COMB_CONST_TYPE_VAR || - value->left->comb.comb_lr != COMB_LR_TYPE_LEFT) + if (value->left->comb.comb_const != COMB_CONST_TYPE_VAR) { *result = TYPECHECK_FAIL; value->comb.comb = COMB_TYPE_ERR; print_error_msg(value->line_no, - "cannot assign to type const %s", + "cannot assign to %s %s", + comb_const_type_str(value->left->comb.comb_const), comb_type_str(value->left->comb.comb)); } + value->comb.comb_const = value->right->comb.comb_const; + if (value->left->comb.comb == COMB_TYPE_BOOL && value->right->comb.comb == COMB_TYPE_BOOL) { @@ -2945,7 +2970,6 @@ int expr_array_deref_array_check_type(symtab * tab, expr * value, { value->comb.comb_const = COMB_CONST_TYPE_CONST; } - value->comb.comb_lr = COMB_LR_TYPE_LEFT; } else { @@ -3010,8 +3034,11 @@ int expr_array_deref_touple_check_type(symtab * tab, expr * value, param * param_value = param_list_get_nth(value->array_deref.array_expr->comb.touple.comb_dims, (unsigned int)index); if (param_value != NULL) { - param_value->const_type = COMB_CONST_TYPE_CONST; expr_set_comb_type(value, param_value); + if (value->array_deref.array_expr->comb.comb_const == COMB_CONST_TYPE_CONST) + { + value->comb.comb_const = COMB_CONST_TYPE_CONST; + } } else { @@ -3091,12 +3118,10 @@ int expr_array_deref_slice_check_type(symtab * tab, expr * value, func_value, syn_level, result) == TYPECHECK_SUCC) { expr_set_comb_type(value, value->array_deref.array_expr->comb.slice.comb_ret); - if (value->array_deref.array_expr->comb.comb_const == COMB_CONST_TYPE_CONST) { value->comb.comb_const = COMB_CONST_TYPE_CONST; } - value->comb.comb_lr = COMB_LR_TYPE_LEFT; } else { @@ -3128,7 +3153,6 @@ int expr_array_deref_string_check_type(symtab * tab, expr * value, { value->comb.comb = COMB_TYPE_CHAR; value->comb.comb_const = COMB_CONST_TYPE_CONST; - value->comb.comb_lr = COMB_LR_TYPE_RIGHT; } else { @@ -3275,8 +3299,7 @@ int expr_range_check_type(symtab * tab, expr * value, func * func_value, expr_range_list_check_type(tab, value->range.range_dims, func_value, syn_level, result); value->comb.comb = COMB_TYPE_RANGE; - value->comb.comb_const = COMB_CONST_TYPE_CONST; - value->comb.comb_lr = COMB_LR_TYPE_RIGHT; + value->comb.comb_const = COMB_CONST_TYPE_TEMP; value->comb.range.comb_dims = value->range.range_dims->count; value->comb.range.comb_ret = value->range.ret; @@ -3360,7 +3383,6 @@ int expr_call_check_type(symtab * tab, expr * value, func * func_value, unsigned value->call.params, true) == TYPECHECK_SUCC) { expr_set_comb_type(value, value->call.func_expr->comb.func.comb_ret); - value->comb.comb_lr = COMB_LR_TYPE_RIGHT; } else { @@ -3375,9 +3397,8 @@ int expr_call_check_type(symtab * tab, expr * value, func * func_value, unsigned value->call.params, false) == TYPECHECK_SUCC) { value->comb.comb = COMB_TYPE_RECORD; + value->comb.comb_const = COMB_CONST_TYPE_TEMP; value->comb.comb_record = value->call.func_expr->comb.comb_record; - value->comb.comb_const = COMB_CONST_TYPE_CONST; - value->comb.comb_lr = COMB_LR_TYPE_RIGHT; } else { @@ -3400,6 +3421,7 @@ int expr_call_check_type(symtab * tab, expr * value, func * func_value, unsigned { value->call.func_expr->enumtype.called = 1; value->comb.comb = COMB_TYPE_ENUMTYPE; + value->comb.comb_const = COMB_CONST_TYPE_TEMP; value->comb.comb_enumtype = value->call.func_expr->comb.comb_enumtype; } else @@ -3631,7 +3653,11 @@ int expr_listcomp_check_type(symtab * tab, listcomp * listcomp_value, if (listcomp_value->ret != NULL) { - param_check_type(listcomp_value->stab, listcomp_value->ret, syn_level, false, PARAM_CONST_TYPE_VAR, result); + if (listcomp_value->ret->const_type == PARAM_CONST_TYPE_DEFAULT) + { + listcomp_value->ret->const_type = PARAM_CONST_TYPE_VAR; + } + param_check_type(listcomp_value->stab, listcomp_value->ret, syn_level, result); } if (param_expr_cmp(listcomp_value->ret, listcomp_value->expr_value, false) @@ -3668,12 +3694,6 @@ int expr_attr_check_type(symtab * tab, expr * value, func * func_value, unsigned value->attr.id->id.id_param_value = param_value; expr_set_comb_type(value, param_value); - - if (value->attr.record_value->comb.comb_const == COMB_CONST_TYPE_CONST) - { - value->comb.comb_const = COMB_CONST_TYPE_CONST; - } - value->comb.comb_lr = COMB_LR_TYPE_LEFT; } else { @@ -3872,7 +3892,6 @@ int expr_check_type(symtab * tab, expr * value, func * func_value, unsigned int { value->comb.comb = COMB_TYPE_INT; value->comb.comb_const = COMB_CONST_TYPE_CONST; - value->comb.comb_lr = COMB_LR_TYPE_RIGHT; } else { @@ -3892,8 +3911,7 @@ int expr_check_type(symtab * tab, expr * value, func * func_value, unsigned int if (value->forloop.cond->comb.comb == COMB_TYPE_BOOL) { value->comb.comb = COMB_TYPE_INT; - value->comb.comb_const = COMB_CONST_TYPE_CONST; - value->comb.comb_lr = COMB_LR_TYPE_RIGHT; + value->comb.comb_const = COMB_CONST_TYPE_TEMP; } else { @@ -3945,8 +3963,9 @@ int expr_check_type(symtab * tab, expr * value, func * func_value, unsigned int expr_list_check_type(tab, value->touple_value->values, func_value, syn_level, result); if (value->touple_value->dims != NULL) - { - param_list_check_type(tab, value->touple_value->dims, syn_level, false, PARAM_CONST_TYPE_VAR, result); + { + param_list_set_default_var(value->touple_value->dims); + param_list_check_type(tab, value->touple_value->dims, syn_level, result); } if (*result == TYPECHECK_FAIL || @@ -4142,14 +4161,13 @@ int bind_check_type(symtab * tab, bind * value, func * func_value, unsigned int switch (value->type) { case BIND_LET: - if (param_expr_cmp_init(PARAM_CONST_TYPE_CONST, value->expr_value) == TYPECHECK_FAIL) - { - *result = TYPECHECK_FAIL; - } break; case BIND_VAR: - if (param_expr_cmp_init(PARAM_CONST_TYPE_VAR, value->expr_value) == TYPECHECK_FAIL) + if (value->expr_value->comb.comb_const == COMB_CONST_TYPE_CONST) { + print_error_msg(value->line_no, "cannot assign %s %s to var", + comb_const_type_str(value->expr_value->comb.comb_const), + comb_type_str(value->expr_value->comb.comb)); *result = TYPECHECK_FAIL; } break; @@ -4338,7 +4356,7 @@ int func_native_check_type(symtab * tab, func * func_value, unsigned int syn_lev expr_check_type(func_value->stab, func_value->body->exprs, func_value, syn_level, result); - if (param_expr_cmp(func_value->decl->ret, func_value->body->exprs, false) == + if (param_expr_cmp(func_value->decl->ret, func_value->body->exprs, true) == TYPECHECK_FAIL) { *result = TYPECHECK_FAIL; @@ -4373,13 +4391,11 @@ int func_param_check_type(symtab * tab, func * func_value, unsigned int syn_leve case FUNC_TYPE_NATIVE: if (func_value->decl->params != NULL) { - param_list_check_type(tab, func_value->decl->params, syn_level, - true, PARAM_CONST_TYPE_CONST, result); + param_list_check_type(tab, func_value->decl->params, syn_level, result); } if (func_value->decl->ret != NULL) { - param_check_type(tab, func_value->decl->ret, syn_level, - false, PARAM_CONST_TYPE_VAR, result); + param_check_type(tab, func_value->decl->ret, syn_level, result); } break; case FUNC_TYPE_UNKNOWN: @@ -4744,12 +4760,13 @@ int record_check_type(symtab * stab, record * record_value, int * result) { if (record_value->params != NULL) { + param_list_set_default_var(record_value->params); record_enum_param_list(record_value->params); } if (record_value->params != NULL) { - param_list_check_type(stab, record_value->params, 0, false, PARAM_CONST_TYPE_VAR, result); + param_list_check_type(stab, record_value->params, 0, result); } if (record_value->stab != NULL && record_value->params != NULL) diff --git a/front/typecheck.h b/front/typecheck.h index 0cf46860..1a87fe97 100644 --- a/front/typecheck.h +++ b/front/typecheck.h @@ -40,6 +40,7 @@ int enum_matchbind_list(matchbind_list * list); int expr_set_comb_type(expr * value, param * param_value); int expr_set_comb_type_symtab(expr * value, symtab_entry * entry, unsigned int syn_level, int * result); +int expr_bind_set_comb_type(expr * value, bind * bind_value); int expr_qualifier_set_comb_type(expr * value, expr * expr_value, int * result); int expr_conv_basic_type(expr * value, expr * expr_left, expr * expr_right); int expr_conv_string_type(expr * value, expr * expr_left, expr * expr_right); @@ -85,22 +86,17 @@ int array_dims_check_type_expr_list(symtab * tab, expr_list * list, int param_enum_record_check_type(symtab * tab, param * param_value, unsigned int syn_level, int * result); int param_range_check_type(symtab * tab, range * value, - unsigned int syn_level, bool change_const_allowed, - param_const_type const_type, int * result); + unsigned int syn_level, int * result); int param_range_list_check_type(symtab * tab, range_list * list, - unsigned int syn_level, - bool change_const_allowed, - param_const_type const_type, int * result); + unsigned int syn_level, int * result); int param_ffi_check_type(symtab * tab, param * param_value, unsigned int syn_level, int * result); int param_check_type(symtab * tab, param * param_value, - unsigned int syn_level, bool change_const_allowed, - param_const_type const_type, int * result); + unsigned int syn_level, int * result); int param_list_ffi_check_type(symtab * tab, param_list * list, unsigned int syn_level, int * result); int param_list_check_type(symtab * tab, param_list * list, - unsigned int syn_level, bool change_const_allowed, - param_const_type const_type, int * result); + unsigned int syn_level, int * result); int expr_id_check_type(symtab * tab, expr * value, unsigned int syn_level, int * result); int expr_enumtype_check_type(symtab * tab, expr * value, func * func_value, unsigned int syn_level, int * result); diff --git a/include/program.h b/include/program.h index 8fe7f85c..9c5530f3 100644 --- a/include/program.h +++ b/include/program.h @@ -29,6 +29,9 @@ typedef struct program object * params; unsigned int params_count; unsigned int entry_addr; + unsigned int msg_count; + unsigned int msg_array_size; + char ** msg_array; struct module * module_value; } program; diff --git a/sample/sample198.nev b/sample/sample198.nev index 931f8e2b..8b4dbfe3 100644 --- a/sample/sample198.nev +++ b/sample/sample198.nev @@ -1,16 +1,15 @@ -func f(i : int) -> [_] : int +func f(i : int) -> var [_] : int { var t2 = i * [ 11, 12, 13, 14 ] : int; - t2 } func main() -> int { var t = f(5); - + t[0] = 120; - + print(t[0]); print(t[1]); print(t[2]); diff --git a/sample/sample247.nev b/sample/sample247.nev index 371b3710..1424e50d 100644 --- a/sample/sample247.nev +++ b/sample/sample247.nev @@ -10,10 +10,10 @@ func print_str(hw[L] : float) -> int func reverse(hw[L] : float) -> [_] : float { var rev = {[ L ]} : float; - + func __reverse(hw[L1] : float, var rev[L2] : float, i : int) -> int { - i < L1 ? { rev[L2 - i - 1] = hw[i]; __reverse(hw, rev, i + 1) } : 0 + i < L1 ? { rev[L2 - i - 1] = hw[i]; __reverse(hw, rev, i + 1) } : 0 }; __reverse(hw, rev, 0); @@ -22,7 +22,7 @@ func reverse(hw[L] : float) -> [_] : float func main() -> int { - var hw = [ 72.0, 101.0, 108.0, 18.0 ] : float; + let hw = [ 72.0, 101.0, 108.0, 18.0 ] : float; print_str(hw); print_str(reverse(hw)) diff --git a/sample/sample281.nev b/sample/sample281.nev index 5e2bf5e2..83034b51 100644 --- a/sample/sample281.nev +++ b/sample/sample281.nev @@ -16,6 +16,6 @@ func gf(v : int) -> (int) -> int func main() -> bool { - var f = gf(87); + let f = gf(87); assert(f(0) == 88) } diff --git a/sample/sample304.nev b/sample/sample304.nev index 5998a2ee..981d70e9 100644 --- a/sample/sample304.nev +++ b/sample/sample304.nev @@ -7,7 +7,7 @@ record Tree { func get_Tree(value : int) -> Tree { - var t = Tree(value, nil, nil); + let t = Tree(value, nil, nil); t } @@ -24,7 +24,7 @@ catch (nil_pointer) func main() -> int { - var t = get_Tree(110); + let t = get_Tree(110); print_Tree(t); print_Tree(get_Tree(120)); diff --git a/sample/sample350.nev b/sample/sample350.nev index 7fcdf58f..41cac07a 100644 --- a/sample/sample350.nev +++ b/sample/sample350.nev @@ -48,7 +48,7 @@ func printRLC( RLC[D] : V ) -> int func main() -> int { - var rlc = testRLC(); + let rlc = testRLC(); printRLC(rlc); diff --git a/sample/sample358.nev b/sample/sample358.nev index 40b8ba83..a658cd21 100644 --- a/sample/sample358.nev +++ b/sample/sample358.nev @@ -38,7 +38,7 @@ func get_primes(n : int) -> [_] : int func main() -> int { - var primes = get_primes(128); + let primes = get_primes(128); print_ints(primes); diff --git a/sample/sample360.nev b/sample/sample360.nev index cfaeb96f..ec176221 100644 --- a/sample/sample360.nev +++ b/sample/sample360.nev @@ -11,10 +11,10 @@ extern "libm.so.6" func atanf(x : float) -> float func main() -> int { - var v1 = sinhf(1.0); - var v2 = coshf(1.0); - var v3 = powf(10.0, 2.0); - var pi = 4.0 * atanf(1.0); + let v1 = sinhf(1.0); + let v2 = coshf(1.0); + let v3 = powf(10.0, 2.0); + let pi = 4.0 * atanf(1.0); printf(v1); printf(v2); diff --git a/sample/sample362.nev b/sample/sample362.nev index cbaab63c..8ddcd45b 100644 --- a/sample/sample362.nev +++ b/sample/sample362.nev @@ -2,8 +2,8 @@ func main() -> int { - var system = let extern "libc.so.6" func system(cmd : string) -> float; - var v = system("uname -a"); + let system = let extern "libc.so.6" func system(cmd : string) -> float; + let v = system("uname -a"); 0 } diff --git a/sample/sample443.nev b/sample/sample443.nev index 55a435d5..d2038af1 100644 --- a/sample/sample443.nev +++ b/sample/sample443.nev @@ -11,7 +11,7 @@ func main() -> int { let f = 10; let t = 30; - let r1 = [ 10 .. 30 ]; + var r1 = [ 10 .. 30 ]; var r2 = [ f .. t ]; print_range( r1 ); diff --git a/sample/sample607.nev b/sample/sample607.nev index 5be3b132..9ca518b8 100644 --- a/sample/sample607.nev +++ b/sample/sample607.nev @@ -6,7 +6,7 @@ func f1(i : int) -> int 0 } -func pT( t[D] : int, var [ F .. T ] : range ) -> int +func pT( t[D] : int, [ F .. T ] : range ) -> int { print(F); print(T); diff --git a/sample/sample609.nev b/sample/sample609.nev index e93e6a74..623ddaff 100644 --- a/sample/sample609.nev +++ b/sample/sample609.nev @@ -7,13 +7,13 @@ record R func getR() -> R { - var r = R(10, 10); + let r = R(10, 10); r } func main() -> int { - var r = getR(); + let r = getR(); r.i = 20; r.j = 30; diff --git a/sample/sample610.nev b/sample/sample610.nev index 13ab279e..ee73e119 100644 --- a/sample/sample610.nev +++ b/sample/sample610.nev @@ -1,5 +1,5 @@ -func f (s : int) -> [D] : int +func f (s : int) -> var [D] : int { var t = [ s, s, s, s, s, s ] : int; t diff --git a/sample/sample615.nev b/sample/sample615.nev index 5b7812a3..b3041422 100644 --- a/sample/sample615.nev +++ b/sample/sample615.nev @@ -13,7 +13,7 @@ func getR(i : int) -> R func main() -> int { var i = 20; - var r = getR(i); + let r = getR(i); func processR(r : R) -> int { diff --git a/sample/sample616.nev b/sample/sample616.nev index bd3aea89..05eb07e4 100644 --- a/sample/sample616.nev +++ b/sample/sample616.nev @@ -1,5 +1,5 @@ -func set(i : int) -> int +func set(var i : int) -> int { var j = i; diff --git a/sample/sample619.nev b/sample/sample619.nev index 17153ffc..d5647fd3 100644 --- a/sample/sample619.nev +++ b/sample/sample619.nev @@ -23,7 +23,7 @@ func getF(f : F) -> (var int) -> int func main() -> int { var i = 0; - var f = getF(F::ONE); + let f = getF(F::ONE); f(i); diff --git a/sample/sample639.nev b/sample/sample639.nev new file mode 100644 index 00000000..e8da32d9 --- /dev/null +++ b/sample/sample639.nev @@ -0,0 +1,14 @@ + + +func main() -> int +{ + var a = 10; + let b = 100; + + a = b; + a = 1000; + + prints("a = " + a + "\n"); + + 0 +} diff --git a/sample/sample640.nev b/sample/sample640.nev new file mode 100644 index 00000000..e3d46d2d --- /dev/null +++ b/sample/sample640.nev @@ -0,0 +1,14 @@ + + +func main() -> int +{ + let a = 10; + var b = 100; + + a = b; + a = 1000; + + prints("a = " + a + "\n"); + + 0 +} diff --git a/sample/sample640.nev.err b/sample/sample640.nev.err new file mode 100644 index 00000000..b4f7482d --- /dev/null +++ b/sample/sample640.nev.err @@ -0,0 +1,2 @@ +:8: error: cannot assign to const int +:9: error: cannot assign to const int diff --git a/sample/sample641.nev b/sample/sample641.nev new file mode 100644 index 00000000..06b6a25e --- /dev/null +++ b/sample/sample641.nev @@ -0,0 +1,20 @@ + +func f1(var a : int, b : int) -> int +{ + var c = a; + let d = b; + + c = 1110 +} + +func main() -> int +{ + var a = 1; + let b = 2; + + f1(a, b + 20); + + prints("a = " + a + " b = " + b + "\n"); + + 0 +} diff --git a/sample/sample642.nev b/sample/sample642.nev new file mode 100644 index 00000000..cc5e1bf5 --- /dev/null +++ b/sample/sample642.nev @@ -0,0 +1,16 @@ + +func f1(var a : int) -> var int +{ + a +} + +func main() -> int +{ + var a = 1; + + f1(a) = 10; + + prints("a = " + a + "\n"); + + 0 +} diff --git a/sample/sample643.nev b/sample/sample643.nev new file mode 100644 index 00000000..4346a43a --- /dev/null +++ b/sample/sample643.nev @@ -0,0 +1,40 @@ + +func test1() -> int +{ + var a = [ 1, 2, 3, 4 ] : int; + var b = [ 10, 20, 30, 40 ] : int; + + a[0] = 1; + a[1] = 2; + a[2] = 3; + a[3] = 4; + + a = b; + /* b[0] = 2200; */ + b = [ 1000, 2000, 3000 ] : int; + + prints("a[0] = " + a[0] + "\n"); + + 0 +} + +func test2() -> int +{ + var c = 10; + var d = [ c ] : int; + + d[0] = 300; + + prints("c = " + c + "\n"); + + 0 +} + +func main() -> int +{ + test1(); + test2(); + + 0 +} + diff --git a/sample/sample644.nev b/sample/sample644.nev new file mode 100644 index 00000000..f865750e --- /dev/null +++ b/sample/sample644.nev @@ -0,0 +1,23 @@ + +record R +{ + var a : int; + var b : int; +} + +func main() -> int +{ + var a = R(10, 20); + let b = R(100, 200); + + a = b; + + b.a = 1000; + b.b = 2000; + + prints("a.a = " + a.a + " a.b = " + a.b + "\n"); + assert(a.a == 1000); + assert(a.b == 2000); + + 0 +} diff --git a/sample/sample645.nev b/sample/sample645.nev new file mode 100644 index 00000000..b4732f1b --- /dev/null +++ b/sample/sample645.nev @@ -0,0 +1,22 @@ + +record R +{ + a : int; + b : int; +} + +func f1(var r : R) -> var int +{ + r.a +} + +func main() -> int +{ + var r1 = R(10, 20); + + f1(r1) = 100; + + prints("r.a = " + r1.a + " r.b = " + r1.b + "\n"); + + 0 +} diff --git a/sample/sample646.nev b/sample/sample646.nev new file mode 100644 index 00000000..7ffad7e0 --- /dev/null +++ b/sample/sample646.nev @@ -0,0 +1,21 @@ + +enum EONE { one, two, three, four } + +func main() -> int +{ + var e1 = EONE::one; + var e2 = e1; + + e2 = e1; + e2 = EONE::four; + + match e1 + { + EONE::one -> prints("EONE::one\n"); + EONE::two -> prints("EONE::two\n"); + EONE::three -> prints("EONE::three\n"); + EONE::four -> prints("EONE::four\n"); + }; + + 0 +} diff --git a/sample/sample647.nev b/sample/sample647.nev new file mode 100644 index 00000000..95611433 --- /dev/null +++ b/sample/sample647.nev @@ -0,0 +1,12 @@ + +func f1( [ from .. to ] : range) -> int +{ + 0 +} + +func main() -> int +{ + f1( [ 10 .. 200] ); + 0 +} + diff --git a/sample/sample648.nev b/sample/sample648.nev new file mode 100644 index 00000000..0090f439 --- /dev/null +++ b/sample/sample648.nev @@ -0,0 +1,24 @@ +enum Optional { Some { value : int; }, None } + +func main() -> int +{ + var a = 1000; + var o1 = Optional::Some(a); + let o2 = Optional::Some(a); + + a = 1; + + match o1 + { + Optional::Some(value) -> value = 900; + Optional::None -> 0; + }; + + match o1 + { + Optional::Some(value) -> prints(value + "\n"); + Optional::None -> prints("None"); + }; + + 0 +} diff --git a/sample/sample649.nev b/sample/sample649.nev new file mode 100644 index 00000000..86a3ef65 --- /dev/null +++ b/sample/sample649.nev @@ -0,0 +1,13 @@ + +func main() -> int +{ + var a = "aaaaaa"; + var b = "bbbbbb"; + + a = b; + b = "ccccccc"; + + prints("a = " + a + "\n"); + + 0 +} diff --git a/sample/sample650.nev b/sample/sample650.nev new file mode 100644 index 00000000..29e20b04 --- /dev/null +++ b/sample/sample650.nev @@ -0,0 +1,15 @@ + +func f1(a : int, b : int) -> int +{ + 0 +} + +func main() -> int +{ + let a = 1; + let b = 2; + + f1(a, b); + + 0 +} diff --git a/sample/sample651.nev b/sample/sample651.nev new file mode 100644 index 00000000..750a07a7 --- /dev/null +++ b/sample/sample651.nev @@ -0,0 +1,15 @@ + +func f1(a : int, b : int) -> int +{ + 0 +} + +func main() -> int +{ + var a = 1; + var b = 2; + + f1(a, b); + + 0 +} diff --git a/sample/sample652.nev b/sample/sample652.nev new file mode 100644 index 00000000..dcfda09f --- /dev/null +++ b/sample/sample652.nev @@ -0,0 +1,15 @@ + +func f1(var a : int, var b : int) -> int +{ + 0 +} + +func main() -> int +{ + var a = 1; + var b = 2; + + f1(a, b); + + 0 +} diff --git a/sample/sample653.nev b/sample/sample653.nev new file mode 100644 index 00000000..3f8f9805 --- /dev/null +++ b/sample/sample653.nev @@ -0,0 +1,15 @@ + +func f1(var a : int, var b : int) -> int +{ + 0 +} + +func main() -> int +{ + let a = 1; + let b = 2; + + f1(a, b); + + 0 +} diff --git a/sample/sample653.nev.err b/sample/sample653.nev.err new file mode 100644 index 00000000..64e786d8 --- /dev/null +++ b/sample/sample653.nev.err @@ -0,0 +1,2 @@ +:12: error: passing const expression to variable param a at line 2 +:12: error: function call type mismatch diff --git a/sample/sample654.nev b/sample/sample654.nev new file mode 100644 index 00000000..c4cefa03 --- /dev/null +++ b/sample/sample654.nev @@ -0,0 +1,17 @@ + +func f1(var a : int) -> var int +{ + a +} + +func main() -> int +{ + var a = 1; + + f1(a) = 10; + + assert(a == 10); + + 0 +} + diff --git a/sample/sample655.nev b/sample/sample655.nev new file mode 100644 index 00000000..918c280e --- /dev/null +++ b/sample/sample655.nev @@ -0,0 +1,17 @@ + +func f1(a : int) -> var int +{ + a +} + +func main() -> int +{ + let a = 1; + + f1(a) = 10; + + assert(a == 10); + + 0 +} + diff --git a/sample/sample655.nev.err b/sample/sample655.nev.err new file mode 100644 index 00000000..facb41fb --- /dev/null +++ b/sample/sample655.nev.err @@ -0,0 +1,2 @@ +:4: error: passing const expression to variable param at line 2 +:2: error: incorrect return type in function f1 diff --git a/sample/sample656.nev b/sample/sample656.nev new file mode 100644 index 00000000..b86f64a9 --- /dev/null +++ b/sample/sample656.nev @@ -0,0 +1,17 @@ + +func f1(var a : int) -> var int +{ + a +} + +func main() -> int +{ + let a = 1; + + f1(a) = 10; + + assert(a == 10); + + 0 +} + diff --git a/sample/sample656.nev.err b/sample/sample656.nev.err new file mode 100644 index 00000000..72e622fe --- /dev/null +++ b/sample/sample656.nev.err @@ -0,0 +1,4 @@ +:11: error: passing const expression to variable param a at line 2 +:11: error: function call type mismatch +:11: error: cannot assign to temp error +:11: error: cannot assign different types error int diff --git a/sample/sample657.nev b/sample/sample657.nev new file mode 100644 index 00000000..e08dabfa --- /dev/null +++ b/sample/sample657.nev @@ -0,0 +1,16 @@ + +func f1(var t[D] : int) -> int +{ + t[0] = 0; + 0 +} + +func main() -> int +{ + var t = [ 1, 2, 3, 4 ] : int; + + f1(t); + + 0 +} + diff --git a/sample/sample658.nev b/sample/sample658.nev new file mode 100644 index 00000000..209ccec3 --- /dev/null +++ b/sample/sample658.nev @@ -0,0 +1,16 @@ + +func f1(var t[D] : int) -> int +{ + t[0] = 0; + 0 +} + +func main() -> int +{ + let t = [ 1, 2, 3, 4 ] : int; + + f1(t); + + 0 +} + diff --git a/sample/sample658.nev.err b/sample/sample658.nev.err new file mode 100644 index 00000000..d3b414fd --- /dev/null +++ b/sample/sample658.nev.err @@ -0,0 +1,2 @@ +:12: error: passing const expression to variable param t at line 2 +:12: error: function call type mismatch diff --git a/sample/sample659.nev b/sample/sample659.nev new file mode 100644 index 00000000..71615c72 --- /dev/null +++ b/sample/sample659.nev @@ -0,0 +1,16 @@ + +func f1(t[D] : int) -> int +{ + t[0] = 0; + 0 +} + +func main() -> int +{ + let t = [ 1, 2, 3, 4 ] : int; + + f1(t); + + 0 +} + diff --git a/sample/sample659.nev.err b/sample/sample659.nev.err new file mode 100644 index 00000000..e96fadc6 --- /dev/null +++ b/sample/sample659.nev.err @@ -0,0 +1 @@ +:4: error: cannot assign to const int diff --git a/sample/sample660.nev b/sample/sample660.nev new file mode 100644 index 00000000..7914dfe3 --- /dev/null +++ b/sample/sample660.nev @@ -0,0 +1,17 @@ + +func f1(var t[D] : int) -> int +{ + D = 10; + t[0] = 0; + 0 +} + +func main() -> int +{ + var t = [ 1, 2, 3, 4 ] : int; + + f1(t); + + 0 +} + diff --git a/sample/sample660.nev.err b/sample/sample660.nev.err new file mode 100644 index 00000000..e96fadc6 --- /dev/null +++ b/sample/sample660.nev.err @@ -0,0 +1 @@ +:4: error: cannot assign to const int diff --git a/sample/sample661.nev b/sample/sample661.nev new file mode 100644 index 00000000..c80e9af3 --- /dev/null +++ b/sample/sample661.nev @@ -0,0 +1,21 @@ + +func f1([ from .. to ] : range) -> int +{ + from = 10; + to = 20; + 0 +} + +func main() -> int +{ + var from = 0; + var to = 0; + + f1( [ from .. to ] ); + + assert(from == 10); + assert(to == 20); + + 0 +} + diff --git a/sample/sample662.nev b/sample/sample662.nev new file mode 100644 index 00000000..d5353360 --- /dev/null +++ b/sample/sample662.nev @@ -0,0 +1,20 @@ + +record R +{ + a : int; + b : int; +} + +func main() -> int +{ + var r1 = R(10, 20); + let r2 = r1; + + r2.a = 100; + r2.b = 200; + + assert(r1.a == 100); + assert(r1.b == 200); + + 0 +} diff --git a/sample/sample663.nev b/sample/sample663.nev new file mode 100644 index 00000000..eab9922c --- /dev/null +++ b/sample/sample663.nev @@ -0,0 +1,20 @@ + +record R +{ + let a : int; + let b : int; +} + +func main() -> int +{ + var r1 = R(10, 20); + let r2 = r1; + + r2.a = 100; + r2.b = 200; + + assert(r1.a == 100); + assert(r1.b == 200); + + 0 +} diff --git a/sample/sample663.nev.err b/sample/sample663.nev.err new file mode 100644 index 00000000..dccdcc11 --- /dev/null +++ b/sample/sample663.nev.err @@ -0,0 +1,2 @@ +:13: error: cannot assign to const int +:14: error: cannot assign to const int diff --git a/sample/sample664.nev b/sample/sample664.nev new file mode 100644 index 00000000..cb316071 --- /dev/null +++ b/sample/sample664.nev @@ -0,0 +1,20 @@ + +record R +{ + a : int; + b : int; +} + +func main() -> int +{ + let r1 = R(10, 20); + let r2 = r1; + + r2.a = 100; + r2.b = 200; + + assert(r1.a == 100); + assert(r1.b == 200); + + 0 +} diff --git a/sample/sample665.nev b/sample/sample665.nev new file mode 100644 index 00000000..c1359a4f --- /dev/null +++ b/sample/sample665.nev @@ -0,0 +1,20 @@ + +record R +{ + a : int; + b : int; +} + +func main() -> int +{ + let r1 = R(10, 20); + var r2 = r1; + + r2.a = 100; + r2.b = 200; + + assert(r1.a == 100); + assert(r1.b == 200); + + 0 +} diff --git a/sample/sample665.nev.err b/sample/sample665.nev.err new file mode 100644 index 00000000..82f67533 --- /dev/null +++ b/sample/sample665.nev.err @@ -0,0 +1 @@ +:11: error: cannot assign const record to var diff --git a/sample/sample666.nev b/sample/sample666.nev new file mode 100644 index 00000000..4f3b11d7 --- /dev/null +++ b/sample/sample666.nev @@ -0,0 +1,24 @@ +enum Optional { Some { let value : int; }, None } + +func main() -> int +{ + var a = 1000; + var o1 = Optional::Some(a); + let o2 = Optional::Some(a); + + a = 1; + + match o1 + { + Optional::Some(value) -> value = 900; + Optional::None -> 0; + }; + + match o1 + { + Optional::Some(value) -> prints(value + "\n"); + Optional::None -> prints("None"); + }; + + 0 +} diff --git a/sample/sample666.nev.err b/sample/sample666.nev.err new file mode 100644 index 00000000..21f0a3df --- /dev/null +++ b/sample/sample666.nev.err @@ -0,0 +1 @@ +:13: error: cannot assign to const int diff --git a/sample/sample667.nev b/sample/sample667.nev new file mode 100644 index 00000000..b5469f13 --- /dev/null +++ b/sample/sample667.nev @@ -0,0 +1,12 @@ + +func main() -> int +{ + var t = (10.0, 20, 30, 40) : (float, int, int, int); + + t[0] = 1.0; + + assert(t[0] == 1.0); + + 0 +} + diff --git a/sample/sample668.nev b/sample/sample668.nev new file mode 100644 index 00000000..ebf20200 --- /dev/null +++ b/sample/sample668.nev @@ -0,0 +1,12 @@ + +func main() -> int +{ + let t = (10.0, 20, 30, 40) : (float, int, int, int); + + t[0] = 1.0; + + assert(t[0] == 1.0); + + 0 +} + diff --git a/sample/sample668.nev.err b/sample/sample668.nev.err new file mode 100644 index 00000000..80f30792 --- /dev/null +++ b/sample/sample668.nev.err @@ -0,0 +1 @@ +:6: error: cannot assign to const float diff --git a/sample/sample669.nev b/sample/sample669.nev new file mode 100644 index 00000000..658d4b8b --- /dev/null +++ b/sample/sample669.nev @@ -0,0 +1,11 @@ + + +func main() -> int +{ + var x = 0; + + x = { 20 }; + assert(x == 20); + + 0 +} diff --git a/sample/sample670.nev b/sample/sample670.nev new file mode 100644 index 00000000..a9396a21 --- /dev/null +++ b/sample/sample670.nev @@ -0,0 +1,11 @@ + + +func main() -> int +{ + let x = 0; + + x = { 20 }; + assert(x == 20); + + 0 +} diff --git a/sample/sample670.nev.err b/sample/sample670.nev.err new file mode 100644 index 00000000..3e264636 --- /dev/null +++ b/sample/sample670.nev.err @@ -0,0 +1 @@ +:7: error: cannot assign to const int diff --git a/sample/sample671.nev b/sample/sample671.nev new file mode 100644 index 00000000..44982594 --- /dev/null +++ b/sample/sample671.nev @@ -0,0 +1,10 @@ + + +func main() -> int +{ + let x = 0; + + x = { 'X' }; + + 0 +} diff --git a/sample/sample671.nev.err b/sample/sample671.nev.err new file mode 100644 index 00000000..0c7194d8 --- /dev/null +++ b/sample/sample671.nev.err @@ -0,0 +1,2 @@ +:7: error: cannot assign to const int +:7: error: cannot assign different types int char diff --git a/sample/sample672.nev b/sample/sample672.nev new file mode 100644 index 00000000..26c73555 --- /dev/null +++ b/sample/sample672.nev @@ -0,0 +1,10 @@ + +func main() -> int +{ + var x = 0; + + { x } = 20; + assert(x == 20); + + 0 +} diff --git a/sample/sample672.nev.err b/sample/sample672.nev.err new file mode 100644 index 00000000..5038ebcc --- /dev/null +++ b/sample/sample672.nev.err @@ -0,0 +1 @@ +:6: warning: expression on assignment left side is not an identifier but seq diff --git a/sample/sample673.nev b/sample/sample673.nev new file mode 100644 index 00000000..1bf96364 --- /dev/null +++ b/sample/sample673.nev @@ -0,0 +1,14 @@ + +func main() -> int +{ + var i = 0; + var s = 0; + + for (i = 1; i <= 10; i = i + 1) + { + s = s + i + } = 55; + print(s); + + 0 +} diff --git a/sample/sample673.nev.err b/sample/sample673.nev.err new file mode 100644 index 00000000..13d909d4 --- /dev/null +++ b/sample/sample673.nev.err @@ -0,0 +1,2 @@ +:10: warning: expression on assignment left side is not an identifier but seq +:10: error: cannot assign to temp int diff --git a/sample/sample674.nev b/sample/sample674.nev new file mode 100644 index 00000000..8ff24576 --- /dev/null +++ b/sample/sample674.nev @@ -0,0 +1,15 @@ + +func main() -> int +{ + var i = 0; + var s = 0; + + while (i <= 10) + { + s = s + i; + i = i + 1 + } = 55; + print(s); + + 0 +} diff --git a/sample/sample674.nev.err b/sample/sample674.nev.err new file mode 100644 index 00000000..e11c8338 --- /dev/null +++ b/sample/sample674.nev.err @@ -0,0 +1,2 @@ +:11: warning: expression on assignment left side is not an identifier but while +:11: error: cannot assign to const int diff --git a/sample/sample675.nev b/sample/sample675.nev new file mode 100644 index 00000000..99d9d631 --- /dev/null +++ b/sample/sample675.nev @@ -0,0 +1,14 @@ + +func main() -> int +{ + var i = 1; + var s = 0; + + do { + s = s + i; + i = i + 1 + } while (i <= 10) = 55; + print(s); + + 0 +} diff --git a/sample/sample675.nev.err b/sample/sample675.nev.err new file mode 100644 index 00000000..21798316 --- /dev/null +++ b/sample/sample675.nev.err @@ -0,0 +1,2 @@ +:10: warning: expression on assignment left side is not an identifier but do while +:10: error: cannot assign to const int diff --git a/sample/sample676.nev b/sample/sample676.nev new file mode 100644 index 00000000..8a7ddfb3 --- /dev/null +++ b/sample/sample676.nev @@ -0,0 +1,7 @@ + +func main() -> int +{ + { 10; 20 } = { 30; 40 }; + + 0 +} diff --git a/sample/sample676.nev.err b/sample/sample676.nev.err new file mode 100644 index 00000000..8033a42d --- /dev/null +++ b/sample/sample676.nev.err @@ -0,0 +1,2 @@ +:4: warning: expression on assignment left side is not an identifier but seq +:4: error: cannot assign to temp int diff --git a/sample/sample677.nev b/sample/sample677.nev new file mode 100644 index 00000000..e9e131ac --- /dev/null +++ b/sample/sample677.nev @@ -0,0 +1,11 @@ + +enum Index { Zero, One, Two, Three, Four } + +func main() -> int +{ + var i = Index::Zero; + + i = 10; + + 0 +} diff --git a/sample/sample677.nev.err b/sample/sample677.nev.err new file mode 100644 index 00000000..434c1da7 --- /dev/null +++ b/sample/sample677.nev.err @@ -0,0 +1 @@ +:8: error: cannot assign different types enum int diff --git a/sample/sample678.nev b/sample/sample678.nev new file mode 100644 index 00000000..43266e86 --- /dev/null +++ b/sample/sample678.nev @@ -0,0 +1,11 @@ + + +func main() -> int +{ + var t = {[ 2, 2 ]} : int; + + t[0,0] = 10; + assert(t[0,0] == 10); + + 0 +} \ No newline at end of file diff --git a/sample/sample679.nev b/sample/sample679.nev new file mode 100644 index 00000000..2326d1d8 --- /dev/null +++ b/sample/sample679.nev @@ -0,0 +1,10 @@ + + +func main() -> int +{ + let t = {[ 2, 2 ]} : int; + + t[0,0] = 10; + + 0 +} \ No newline at end of file diff --git a/sample/sample679.nev.err b/sample/sample679.nev.err new file mode 100644 index 00000000..3e264636 --- /dev/null +++ b/sample/sample679.nev.err @@ -0,0 +1 @@ +:7: error: cannot assign to const int diff --git a/sample/sample680.nev b/sample/sample680.nev new file mode 100644 index 00000000..20b81226 --- /dev/null +++ b/sample/sample680.nev @@ -0,0 +1,10 @@ + + +func main() -> int +{ + var t = {[ 2, 2 ]} : let int; + + t[0,0] = 10; + + 0 +} \ No newline at end of file diff --git a/sample/sample680.nev.err b/sample/sample680.nev.err new file mode 100644 index 00000000..3e264636 --- /dev/null +++ b/sample/sample680.nev.err @@ -0,0 +1 @@ +:7: error: cannot assign to const int diff --git a/sample/sample681.nev b/sample/sample681.nev new file mode 100644 index 00000000..8d49e001 --- /dev/null +++ b/sample/sample681.nev @@ -0,0 +1,10 @@ + + +func main() -> int +{ + let t = {[ 2, 2 ]} : let int; + + t[0,0] = 10; + + 0 +} \ No newline at end of file diff --git a/sample/sample681.nev.err b/sample/sample681.nev.err new file mode 100644 index 00000000..3e264636 --- /dev/null +++ b/sample/sample681.nev.err @@ -0,0 +1 @@ +:7: error: cannot assign to const int diff --git a/sample/sample682.nev b/sample/sample682.nev new file mode 100644 index 00000000..8b030c55 --- /dev/null +++ b/sample/sample682.nev @@ -0,0 +1,10 @@ + +func main() -> int +{ + var s = [ x * x | x in [10, 20, 30, 40, 50] : int ] : int; + + s[0] = 10; + assert(s[0] == 10); + + 0 +} diff --git a/sample/sample683.nev b/sample/sample683.nev new file mode 100644 index 00000000..4a3f8159 --- /dev/null +++ b/sample/sample683.nev @@ -0,0 +1,9 @@ + +func main() -> int +{ + let s = [ x * x | x in [10, 20, 30, 40, 50] : int ] : int; + + s[0] = 10; + + 0 +} diff --git a/sample/sample683.nev.err b/sample/sample683.nev.err new file mode 100644 index 00000000..ef1dc9e7 --- /dev/null +++ b/sample/sample683.nev.err @@ -0,0 +1 @@ +:6: error: cannot assign to const int \ No newline at end of file diff --git a/sample/sample684.nev b/sample/sample684.nev new file mode 100644 index 00000000..8a97efc6 --- /dev/null +++ b/sample/sample684.nev @@ -0,0 +1,9 @@ + +func main() -> int +{ + var s = [ x * x | x in [10, 20, 30, 40, 50] : int ] : let int; + + s[0] = 10; + + 0 +} diff --git a/sample/sample684.nev.err b/sample/sample684.nev.err new file mode 100644 index 00000000..55070889 --- /dev/null +++ b/sample/sample684.nev.err @@ -0,0 +1 @@ +:6: error: cannot assign to const int diff --git a/sample/sample685.nev b/sample/sample685.nev new file mode 100644 index 00000000..c5b5ee14 --- /dev/null +++ b/sample/sample685.nev @@ -0,0 +1,10 @@ + +func main() -> int +{ + var s = [ x * x | x in [10, 20, 30, 40, 50] : int ] : var int; + + s[0] = 10; + assert(s[0] == 10); + + 0 +} diff --git a/sample/sample686.nev b/sample/sample686.nev new file mode 100644 index 00000000..83166ffd --- /dev/null +++ b/sample/sample686.nev @@ -0,0 +1,19 @@ + +enum Result { Ok { value : int; }, Err { msg : string; }, Wsg } + +func main() -> int +{ + let r = Result::Ok(12); + + if let (Result::Ok(value) = r) + { + print(r) + } + else + { + assert(false); + 0 + } = 12; + + 0 +} diff --git a/sample/sample686.nev.err b/sample/sample686.nev.err new file mode 100644 index 00000000..232cfd4a --- /dev/null +++ b/sample/sample686.nev.err @@ -0,0 +1,2 @@ +:16: warning: expression on assignment left side is not an identifier but seq +:16: error: cannot assign to temp int diff --git a/sample/sample687.nev b/sample/sample687.nev new file mode 100644 index 00000000..12ba41b0 --- /dev/null +++ b/sample/sample687.nev @@ -0,0 +1,16 @@ + +enum Result { Ok { let value : int; }, Err { msg : string; }, Wsg } + +func main() -> int +{ + let r = Result::Ok(12); + + match (r) + { + Result::Ok(value) -> value = 12; + Result::Err(msg) -> { prints(msg); 0 }; + Result::Wsg -> print(r); + }; + + 0 +} diff --git a/sample/sample687.nev.err b/sample/sample687.nev.err new file mode 100644 index 00000000..a2fd9bd9 --- /dev/null +++ b/sample/sample687.nev.err @@ -0,0 +1 @@ +:10: error: cannot assign to const int \ No newline at end of file diff --git a/sample/sample688.nev b/sample/sample688.nev new file mode 100644 index 00000000..7adc7fa9 --- /dev/null +++ b/sample/sample688.nev @@ -0,0 +1,16 @@ + +enum Result { Ok { value : int; }, Err { msg : string; }, Wsg } + +func main() -> int +{ + let r = Result::Ok(12); + + match (r) + { + Result::Ok(value) -> print(value); + Result::Err(msg) -> { prints(msg); 0 }; + Result::Wsg -> print(r); + } = 12; + + 0 +} diff --git a/sample/sample688.nev.err b/sample/sample688.nev.err new file mode 100644 index 00000000..9f66f7bf --- /dev/null +++ b/sample/sample688.nev.err @@ -0,0 +1,2 @@ +:13: warning: expression on assignment left side is not an identifier but match +:13: error: cannot assign to temp int diff --git a/sample/sample689.nev b/sample/sample689.nev new file mode 100644 index 00000000..27937219 --- /dev/null +++ b/sample/sample689.nev @@ -0,0 +1,25 @@ + +enum Result { Ok { var value : int; }, Err { msg : string; }, Wsg } + +func main() -> int +{ + let r = Result::Ok(12); + + match (r) + { + Result::Ok(value) -> value = 18; + Result::Err(msg) -> { prints(msg); 0 }; + Result::Wsg -> print(r); + }; + + if let (Result::Ok(value) = r) + { + assert(value == 18) + } + else + { + assert(false) + }; + + 0 +} diff --git a/sample/sample690.nev b/sample/sample690.nev new file mode 100644 index 00000000..440e3201 --- /dev/null +++ b/sample/sample690.nev @@ -0,0 +1,12 @@ + +func main() -> int +{ + let f1 = let func() -> int { 1000 }; + let f2 = let func() -> int { 2000 }; + + f2 = f1; + assert(f2() == 1000); + assert(f1() == 1000); + + 0 +} diff --git a/sample/sample690.nev.err b/sample/sample690.nev.err new file mode 100644 index 00000000..5d87b4af --- /dev/null +++ b/sample/sample690.nev.err @@ -0,0 +1 @@ +:7: error: cannot assign to const func diff --git a/sample/sample691.nev b/sample/sample691.nev new file mode 100644 index 00000000..5d86e451 --- /dev/null +++ b/sample/sample691.nev @@ -0,0 +1,12 @@ + +func main() -> int +{ + let f1 = let func() -> int { 1000 }; + var f2 = let func() -> int { 2000 }; + + f2 = f1; + assert(f2() == 1000); + assert(f1() == 1000); + + 0 +} diff --git a/sample/sample692.nev b/sample/sample692.nev new file mode 100644 index 00000000..f0705ab4 --- /dev/null +++ b/sample/sample692.nev @@ -0,0 +1,12 @@ + +func main() -> int +{ + var f1 = let func() -> int { 1000 }; + let f2 = let func() -> int { 2000 }; + + f2 = f1; + assert(f2() == 1000); + assert(f1() == 1000); + + 0 +} diff --git a/sample/sample692.nev.err b/sample/sample692.nev.err new file mode 100644 index 00000000..5d87b4af --- /dev/null +++ b/sample/sample692.nev.err @@ -0,0 +1 @@ +:7: error: cannot assign to const func diff --git a/sample/sample693.nev b/sample/sample693.nev new file mode 100644 index 00000000..33e230c4 --- /dev/null +++ b/sample/sample693.nev @@ -0,0 +1,12 @@ + +func main() -> int +{ + var f1 = let func() -> int { 1000 }; + var f2 = let func() -> int { 2000 }; + + f2 = f1; + assert(f2() == 1000); + assert(f1() == 1000); + + 0 +} diff --git a/sample/sample_qsort.nev b/sample/sample_qsort.nev index ac240e8f..bbebb5fe 100644 --- a/sample/sample_qsort.nev +++ b/sample/sample_qsort.nev @@ -74,7 +74,7 @@ func qsort_rec(var tab[D] : int, func qsort(var tab[D] : int) -> int { - var rand = randomize(6); + let rand = randomize(6); qsort_rec(tab, 0, D, rand) } diff --git a/sample/sample_wave.nev b/sample/sample_wave.nev index ba7d6683..e981c2b8 100644 --- a/sample/sample_wave.nev +++ b/sample/sample_wave.nev @@ -105,7 +105,7 @@ func tail() -> int func generate(phi : float) -> int { - var data = get_wave(phi, 24); + let data = get_wave(phi, 24); head(); print_wave(data); diff --git a/test/test_exec.c b/test/test_exec.c index 424cdf9e..f9b0c553 100644 --- a/test/test_exec.c +++ b/test/test_exec.c @@ -31,6 +31,8 @@ #define PATH_MAX 255 #endif +#define MAX_LINE_LEN 1024 + char * readall(const char * file) { FILE * f = fopen(file, "r"); @@ -51,6 +53,47 @@ char * readall(const char * file) return src; } +int check_errors(const char * sample_path, program * prog) +{ + char error_file[PATH_MAX] = { 0 }; + + snprintf(error_file, PATH_MAX, "%s.err", sample_path); + FILE * f = fopen(error_file, "r"); + if (f == NULL) + { + return 1; + } + + char * ret = NULL; + unsigned int msg_idx = 0; + do + { + char line[MAX_LINE_LEN] = { 0 }; + ret = fgets(line, MAX_LINE_LEN, f); + if (ret != NULL && + msg_idx < prog->msg_count) + { + line[strcspn(line, "\n")] = 0; + printf("line: %s", line); + char * substr = strstr(prog->msg_array[msg_idx], line); + if (substr == NULL) + { + break; + } + msg_idx++; + } + } while (ret != NULL && + msg_idx < prog->msg_count); + + fclose(f); + + if (msg_idx >= prog->msg_count) { + return 0; + } + + return 1; +} + void run(program * prog, vm * machine, const char * entry_name, int param1, int param2) { int ret; @@ -191,6 +234,7 @@ void test_sample(const char * samplepath, unsigned int current, unsigned int cou if (ret != 0) { printf("[%d/%d] path: %s\nprog_str: %s\n", current, count, samplepath, prog_str); + ret = check_errors(samplepath, prog); } assert(ret == 0); @@ -218,9 +262,23 @@ void test_sample(const char * samplepath, unsigned int current, unsigned int cou int skip_dir(struct dirent * ent) { - return strcmp(".", ent->d_name) == 0 || + int skip = 0; + + skip = strcmp(".", ent->d_name) == 0 || strcmp("..", ent->d_name) == 0 || strcmp("lib", ent->d_name) == 0; + if (skip) + { + return skip; + } + + char * dot = strrchr(ent->d_name, '.'); + if (dot && strcmp(dot, ".err") == 0) + { + return 1; + } + + return skip; } void test_samples(const char * dirpath)