commit 119b3d86b933c4ad97d529501e089eab788b1c13
parent 085a1dc565b0b0917f4fb6a231e279a6d9d7b9b7
Author: thing1 <thing1@seacrossedlovers.xyz>
Date: Sun, 22 Mar 2026 15:05:55 +0000
changed things
Diffstat:
| M | comp.y | | | 70 | +++++++++++++++++++++++++++++++++++++++++++++++++++++----------------- |
| A | lex.h | | | 25 | +++++++++++++++++++++++++ |
2 files changed, 78 insertions(+), 17 deletions(-)
diff --git a/comp.y b/comp.y
@@ -1,12 +1,10 @@
%{
#include <stdio.h>
-#include "comp.h"
-struct Pos {int row, col;};
-struct Token {struct Pos pos;};
+#include "lex.h"
+#include "comp.h"
extern int yylex();
-extern struct Token lasttok;
void yyerror(const char *msg);
@@ -20,6 +18,11 @@ void yyerror(const char *msg);
Value *value;
}
+%token FUNC
+%token NAME
+%token INT
+%token FLOAT
+
%type <func> func
%type <type> type
%type <rhs> name
@@ -27,11 +30,6 @@ void yyerror(const char *msg);
%type <expr> expr
%type <value> value
-%token FUNC
-%token NAME
-%token INT
-%token FLOAT
-
%left '(' ')'
%left '*' '/'
%left '+' '-'
@@ -68,16 +66,54 @@ params : rhs
;
rhs : value
- | name '(' params ')'
- | rhs '+' rhs
- | rhs '-' rhs
- | rhs '*' rhs
- | rhs '/' rhs
- | '(' rhs ')'
+ | name '(' params ')' {
+ $$ = malloc(sizeof(Rhs));
+ $$->rhs = FCALL_RHS;
+ $$->fcall.name = $1;
+ }
+ | rhs '+' rhs {
+ $$ = malloc(sizeof(Rhs));
+ $$->rhs = MATH;
+ $$->math.op = ADD;
+ $$->math.lhs = $1
+ $$->math.rhs = $3;
+ }
+ | rhs '-' rhs {
+ $$ = malloc(sizeof(Rhs));
+ $$->rhs = MATH;
+ $$->math.op = SUB;
+ $$->math.lhs = $1
+ $$->math.rhs = $3;
+ }
+ | rhs '*' rhs {
+ $$ = malloc(sizeof(Rhs));
+ $$->rhs = MATH;
+ $$->math.op = MUL;
+ $$->math.lhs = $1
+ $$->math.rhs = $3;
+ }
+ | rhs '/' rhs {
+ $$ = malloc(sizeof(Rhs));
+ $$->rhs = MATH;
+ $$->math.op = DIV;
+ $$->math.lhs = $1
+ $$->math.rhs = $3;
+ }
+ | '(' rhs ')' {
+ $$ = malloc(sizeof(Rhs));
+ $$->rhs = BRACE;
+ $$->brace = $2;
+ }
;
-value : INT
- | FLOAT
+value : INT {
+ $$ = malloc(sizeof(Value));
+ memcpy($$, &(Value){atoll(lasttok.data), 0}, sizeof(Value));
+ }
+ | FLOAT {
+ $$ = malloc(sizeof(Value));
+ memcpy($$, &(Value){0, atof(lasttok.data)}, sizeof(Value));
+ }
;
%%
diff --git a/lex.h b/lex.h
@@ -0,0 +1,25 @@
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+struct Pos {
+ int row, col;
+};
+
+struct Token {
+ struct Pos pos;
+ short type;
+ void *data;
+ void *dataend;
+};
+
+struct Lexer {
+ struct Pos pos;
+ char *input;
+ struct Token (*f)();
+};
+
+extern struct Lexer lex;
+extern struct Token lasttok;
+
+extern int yylex();