From ab051248e8a4c7754b2995a0eda14d4e881b824e Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Mon, 25 Jun 2001 17:15:56 +0000 Subject: [PATCH] expr.h: add prototype for print_expr expr.c: add print_expr correct string accessors currect the result type for unary operators qc-lex.l: correct string, vector and quaternion parsing qc-parse.y: precedence corrections and more function scope work CustomTF gets through the parsing again. --- tools/qfcc/include/expr.h | 1 + tools/qfcc/source/expr.c | 70 +++++++++++++++++++++++++++++++++--- tools/qfcc/source/qc-lex.l | 18 +++++----- tools/qfcc/source/qc-parse.y | 20 +++++++---- 4 files changed, 91 insertions(+), 18 deletions(-) diff --git a/tools/qfcc/include/expr.h b/tools/qfcc/include/expr.h index 2553ff045..1ed32f696 100644 --- a/tools/qfcc/include/expr.h +++ b/tools/qfcc/include/expr.h @@ -34,6 +34,7 @@ typedef struct expr_s { } expr_t; expr_t *new_expr (void); +void print_expr (expr_t *e); expr_t *binary_expr (int op, expr_t *e1, expr_t *e2); expr_t *unary_expr (int op, expr_t *e); expr_t *function_expr (expr_t *e1, expr_t *e2); diff --git a/tools/qfcc/source/expr.c b/tools/qfcc/source/expr.c index a1ab2c192..098378f21 100644 --- a/tools/qfcc/source/expr.c +++ b/tools/qfcc/source/expr.c @@ -63,6 +63,68 @@ new_expr () return calloc (1, sizeof (expr_t)); } +void +print_expr (expr_t *e) +{ + printf (" "); + switch (e->type) { + case ex_statement: + break; + case ex_expr: + print_expr (e->e.expr.e1); + if (e->e.expr.op == 'c') { + expr_t *p = e->e.expr.e2; + printf ("("); + while (p) { + print_expr (p); + if (p->next) + printf (","); + p = p->next; + } + printf (")"); + } else { + print_expr (e->e.expr.e2); + if (isprint (e->e.expr.op)) { + printf (" %c", e->e.expr.op); + } else { + printf (" %d", e->e.expr.op); + } + } + break; + case ex_uexpr: + print_expr (e->e.expr.e1); + if (isprint (e->e.expr.op)) { + printf (" u%c", e->e.expr.op); + } else { + printf (" u%d", e->e.expr.op); + } + break; + case ex_def: + printf ("%s", e->e.def->name); + break; + case ex_int: + printf ("%d", e->e.int_val); + break; + case ex_float: + printf ("%g", e->e.float_val); + break; + case ex_string: + printf ("\"%s\"", strings + e->e.string_val); + break; + case ex_vector: + printf ("'%g", e->e.vector_val[0]); + printf ( " %g", e->e.vector_val[1]); + printf ( " %g'", e->e.vector_val[2]); + break; + case ex_quaternion: + printf ("'%g", e->e.quaternion_val[0]); + printf (" %g", e->e.quaternion_val[1]); + printf (" %g", e->e.quaternion_val[2]); + printf (" %g'", e->e.quaternion_val[3]); + break; + } +} + static expr_t * do_op_string (int op, expr_t *e1, expr_t *e2) { @@ -70,8 +132,8 @@ do_op_string (int op, expr_t *e1, expr_t *e2) char *buf; char *s1, *s2; - s1 = G_STRING(e1->e.string_val); - s2 = G_STRING(e2->e.string_val); + s1 = strings + e1->e.string_val; + s2 = strings + e2->e.string_val; switch (op) { case '+': @@ -367,7 +429,7 @@ unary_expr (int op, expr_t *e) case ex_def: { expr_t *n = new_expr (); - n->type = ex_expr; + n->type = ex_uexpr; n->e.expr.op = op; n->e.expr.type = (e->type == ex_def) ? e->e.def->type @@ -405,7 +467,7 @@ unary_expr (int op, expr_t *e) case ex_def: { expr_t *n = new_expr (); - n->type = ex_expr; + n->type = ex_uexpr; n->e.expr.op = op; n->e.expr.type = &type_float; n->e.expr.e1 = e; diff --git a/tools/qfcc/source/qc-lex.l b/tools/qfcc/source/qc-lex.l index 5570b8ec2..0dd930977 100644 --- a/tools/qfcc/source/qc-lex.l +++ b/tools/qfcc/source/qc-lex.l @@ -62,22 +62,24 @@ m ([\-+]?) {ID} return type_or_name(yytext); \"(\\.|[^"])*\" { - yylval.string_val = strdup (yytext); + int len = strlen (yytext) - 2; + yylval.string_val = malloc (len + 1); + strncpy (yylval.string_val, yytext + 1, len); + yylval.string_val[len] = 0; return STRING_VAL; } '{s}*{m}{NUM}{s}+{m}{NUM}{s}+{m}{NUM}{s}*' { - sscanf (yytext, "%f %f %f", &yylval.vector_val[0], - &yylval.vector_val[1], - &yylval.vector_val[2]); + sscanf (yytext, "' %f %f %f '", + &yylval.vector_val[0], &yylval.vector_val[1], + &yylval.vector_val[2]); return VECTOR_VAL; } '{s}*{m}{NUM}{s}+{m}{NUM}{s}+{m}{NUM}{s}+{m}{NUM}{s}*' { - sscanf (yytext, "%f %f %f %f", &yylval.vector_val[0], - &yylval.vector_val[1], - &yylval.vector_val[2], - &yylval.vector_val[3]); + sscanf (yytext, "' %f %f %f %f '", + &yylval.vector_val[0], &yylval.vector_val[1], + &yylval.vector_val[2], &yylval.vector_val[3]); return VECTOR_VAL; } diff --git a/tools/qfcc/source/qc-parse.y b/tools/qfcc/source/qc-parse.y index d3c41d7f5..147d88da1 100644 --- a/tools/qfcc/source/qc-parse.y +++ b/tools/qfcc/source/qc-parse.y @@ -20,7 +20,7 @@ void PR_PrintType(type_t*); type_t *parse_params (def_t *parms); function_t *new_function (void); -void build_scope (def_t *func, def_t *parms); +void build_scope (def_t *func); typedef struct { type_t *type; @@ -48,8 +48,9 @@ typedef struct { %left EQ NE LE GE LT GT %left '+' '-' %left '*' '/' '&' '|' -%left '!' '.' +%left '!' %right '(' +%left '.' %token NAME STRING_VAL %token INT_VAL @@ -130,7 +131,6 @@ maybe_func } param_list { - $$ = param_scope.scope_next; PR_FlushScope (¶m_scope); current_type = $2.type; param_scope.scope_next = $2.pscope; @@ -138,7 +138,7 @@ maybe_func } ')' { - $$ = parse_params ($4); + $$ = parse_params ($3); } | '(' ')' { @@ -230,6 +230,7 @@ begin_function : /*empty*/ { pr_scope = current_def; + build_scope (current_def); } ; @@ -300,9 +301,9 @@ expr | expr '/' expr { $$ = binary_expr ('/', $1, $3); } | expr '&' expr { $$ = binary_expr ('&', $1, $3); } | expr '|' expr { $$ = binary_expr ('|', $1, $3); } - | expr '.' expr { $$ = binary_expr ('.', $1, $3); } | expr '(' arg_list ')' { $$ = function_expr ($1, $3); } | expr '(' ')' { $$ = function_expr ($1, 0); } + | expr '.' expr { $$ = binary_expr ('.', $1, $3); } | '-' expr { $$ = unary_expr ('-', $2); } | '!' expr { $$ = unary_expr ('!', $2); } | NAME @@ -394,8 +395,15 @@ parse_params (def_t *parms) } void -build_scope (def_t *func, def_t *parms) +build_scope (def_t *func) { + int i; + def_t *def; + type_t *ftype = func->type; + + for (i = 0; i < ftype->num_parms; i++) { + def = PR_GetDef (ftype->parm_types[i], pr_parm_names[i], func, &func->num_locals); + } } function_t *