add move & compute stmts

This commit is contained in:
vel 2024-11-14 11:23:37 -08:00
parent fc6c46c8b8
commit 4ee4bf6919
Signed by: velvox
GPG Key ID: 59D9762F674151DF
3 changed files with 20 additions and 5 deletions

View File

@ -85,7 +85,9 @@ struct expr *expr_create_float_literal(float value) {
struct expr *expr_create_string_literal(const char *value) {
struct expr *e = expr_create(EXPR_STRING_LITERAL, 0, 0);
e->string_literal = value;
char *dest = malloc(sizeof(*value));
strcpy(dest, value); // copy contents of source to dest
e->string_literal = dest;
return e;
}

View File

@ -20,6 +20,7 @@ typedef enum {
EXPR_ADD,
EXPR_ARRAY,
EXPR_DIVIDE,
EXPR_EXPONENTIAL,
EXPR_EQUAL_EQUAL,
EXPR_FLOAT_LITERAL,
EXPR_GREATER_THAN,
@ -60,7 +61,16 @@ struct decl {
struct decl *next;
};
typedef enum { STMT_BLOCK, STMT_DECL, STMT_EXPR, STMT_IF, STMT_PRINT, STMT_SECTION } stmt_t;
typedef enum {
STMT_BLOCK,
STMT_DECL,
STMT_EXPR,
STMT_IF,
STMT_PRINT,
STMT_SECTION,
STMT_COMPUTE,
STMT_MOVE
} stmt_t;
struct stmt {
stmt_t kind;

View File

@ -158,7 +158,7 @@ math_op : TOKEN_ADD
mathmaticalexpr : type_expr
{$$ = $1;}
| mathmaticalexpr math_op term
{$$ = expr_create($2->kind, $1, $3);}
| container_expr
{$$ = $1;}
| type_expr container_expr
@ -171,25 +171,28 @@ booleanexpr : mathmaticalexpr TOKEN_LESS_THAN term
| mathmaticalexpr TOKEN_GREATER_THAN term
{$$ = expr_create(EXPR_GREATER_THAN, $1, $3);}
| mathmaticalexpr TOKEN_EQUAL term
{$$ = expr_create(EXPR_EQUAL, $1, $3);}
{$$ = expr_create(EXPR_EQUAL_EQUAL, $1, $3);}
;
type_expr : TOKEN_IDENT
{$$ = expr_create_name(yytext);}
| TOKEN_INTEGER
{$$ = expr_create_integer_literal(atoi(yytext));}
| TOKEN_STRING
{char *str = malloc(strlen(yytext) + 1); strcpy(str, yytext); $$ = expr_create_string_literal(str);}
{$$ = expr_create_string_literal(yytext);}
| TOKEN_SPACE
{$$ = expr_create_integer_literal(0);}
| TOKEN_SUB TOKEN_IDENT
| ext_function
;
ext_function : TOKEN_KEYWORD_FUNCTION TOKEN_IDENT TOKEN_LEFT_PARENTHESIS TOKEN_IDENT TOKEN_RIGHT_PARENTHESIS
;
cbl_function : TOKEN_DISPLAY
{$$ = stmt_create(STMT_PRINT, NULL, NULL, NULL, NULL, NULL, NULL, NULL);}
| TOKEN_MOVE
{$$ = stmt_create(STMT_MOVE, NULL, NULL, NULL, NULL, NULL, NULL, NULL);}
| TOKEN_KEYWORD_COMPUTE
{$$ = stmt_create(STMT_COMPUTE, NULL, NULL, NULL, NULL, NULL, NULL, NULL);}
;
if_branch : TOKEN_IF booleanexpr
;