Begin semantics

This commit is contained in:
Dane Johnson 2021-11-26 13:04:08 -06:00
parent acf66e1816
commit 4aff13c332
4 changed files with 38 additions and 7 deletions

View File

@ -1,3 +1,4 @@
#include "syntax.h"
#include "lexer.h"
#include "parser.h"

View File

@ -1,4 +1,5 @@
%{
#include "syntax.h"
#include "parser.h"
int stack[100]; // Max indentation depth
int sp = -1;

View File

@ -8,14 +8,19 @@ int yyerror(const char* p) { fprintf(stderr, p); }
%define parse.trace
%union {
expr_t expr;
binop_t binop;
atom_t id;
num_t num;
char *sym;
float num;
string_t string;
}
%token <sym> ID
%token STOP
%token <id> ID
%token <num> NUM
%token STRING
%token <string> STRING
%token STOP
%token INDENT DEDENT
%token IF ELIF ELSE
@ -25,6 +30,8 @@ int yyerror(const char* p) { fprintf(stderr, p); }
%left '/' '*'
%left MAPS
%nterm <expr> expr
%%
program: statements | statements statement;
statements: statements statement STOP
@ -41,8 +48,10 @@ expr: funcdef
| expr '-' expr
| expr '*' expr
| expr '/' expr
| '(' expr ')'
| ID | NUM | STRING;
| '(' expr ')' {$$ = $2;}
| ID {$$.id = $1; $$.type = ID;}
| NUM {$$.num = $1; $$.type = NUM;}
| STRING {$$.string = $1; $$.type = STRING;};
funcdef: param MAPS expr | param MAPS block;
param: ID | '_' | // null production;

View File

@ -1,6 +1,26 @@
#ifndef SYNTAX_H
#define SYNTAX_H
typedef float num_t;
typedef char* atom_t;
typedef char* string_t;
typedef struct expr_t expr_t;
typedef struct binop_t binop_t;
struct expr_t {
int type;
union {
atom_t id;
num_t num;
string_t string;
binop_t *binop;
};
};
struct binop_t {
expr_t *first;
expr_t *second;
int op;
};
#endif /* SYNTAX_H */