commit 56f52b2c4841ff07db198cf96cbc5006d6ed08f8
parent 6b153cf8e8da051a26ce157e7632641e87341555
Author: thing1 <thing1@seacrossedlovers.xyz>
Date: Wed, 25 Mar 2026 11:21:54 +0000
let names be more than 1 char long
Diffstat:
3 files changed, 28 insertions(+), 19 deletions(-)
diff --git a/comp.h b/comp.h
@@ -7,10 +7,11 @@ typedef struct Rhs Rhs;
typedef struct Expr Expr;
typedef struct Value Value;
typedef struct Arg Arg;
+typedef struct Name Name;
typedef struct Func {
Type *type;
- char *name;
+ Name *name;
Arg *args;
int argc;
Expr *body;
@@ -50,11 +51,11 @@ typedef struct Expr {
union {
struct {
Type *type;
- char *name;
+ Name *name;
Rhs *rhs;
} assign;
struct {
- char *name;
+ Name *name;
Rhs *args;
int argc;
} fcall;
@@ -84,7 +85,7 @@ typedef struct Rhs {
Rhs *rhs;
} math;
struct {
- char *name;
+ Name *name;
Rhs **args;
int argc;
} fcall;
@@ -108,6 +109,6 @@ typedef struct List {
} List;
typedef struct Arg {
- char *name;
+ Name *name;
Type *type;
} Arg;
diff --git a/comp.y b/comp.y
@@ -16,6 +16,7 @@ Func magic = {0};
#define APPEND(l, v) l->count++; \
l->data = (l->data) ? realloc(l->data, l->size * l->count) : malloc(l->size); \
memcpy(l->data + l->size * (l->count - 1), v, l->size);
+#define STR(v, t) v = calloc(1, t->dataend - t->data + 1); memcpy(v, t->data, t->dataend - t->data)
%}
%union {
@@ -62,12 +63,15 @@ prog : func
;
func : FUNC name '(' args ')' type
- '{' exprs '}' { NEW($$, ((Func){.type = $6, .args = (Arg *)$4->data, .argc = $4->count, .name = $2->name, .body = (Expr *)$8->data, .exprc = $8->count})); }
+ '{' exprs '}' { NEW($$, ((Func){.type = $6, .args = (Arg *)$4->data, .argc = $4->count, .name = $2, .body = (Expr *)$8->data, .exprc = $8->count})); }
| FUNC name '(' ')' type
- '{' exprs '}' { NEW($$, ((Func){.type = $5, .name = $2->name, .body = (Expr *)$7->data, .exprc = $7->count})); magic = *$$;}
+ '{' exprs '}' { NEW($$, ((Func){.type = $5, .name = $2, .body = (Expr *)$7->data, .exprc = $7->count})); magic = *$$;}
;
-name : NAME { NEW($$, ((Name){.name = $1->data})); }
+name : NAME {
+ NEW($$, ((Name){.name = NULL}));
+ STR($$->name, $1);
+ }
;
args : arg {
@@ -80,11 +84,11 @@ args : arg {
}
;
-arg : name type { NEW($$, ((Arg){.name = $1->name, .type = $2})); }
+arg : name type { NEW($$, ((Arg){.name = $1, .type = $2})); }
-type : NAME {
+type : name {
enum Types ty = 0;
- switch (((char *)$1->data)[0]) {
+ switch (((char *)$1->name)[0]) {
case 'i': break;
case 'u': ty |= 16; break;
case 'f': ty |= 32; break;
@@ -92,7 +96,7 @@ type : NAME {
default: yyerror("Invalid type");
}
- switch (((char *)$1->data)[1]) {
+ switch (((char *)$1->name)[1]) {
case '8': ty = 1; break;
case '1': ty = 2; break;
case '3': ty = 4; break;
@@ -112,8 +116,8 @@ exprs : expr ';' {
| exprs expr ';' { APPEND($1, $2); }
;
-expr : name '(' params ')' { NEW($$, ((Expr){.expr = FCALL_EXPR, .fcall.name = $1->name, .fcall.args = (Rhs *)$3->data, .fcall.argc = $3->count})); }
- | VAR name type '=' rhs { NEW($$, ((Expr){.expr = ASSIGN, .assign.type = $3, .assign.name = $2->name, .assign.rhs = $5})); }
+expr : name '(' params ')' { NEW($$, ((Expr){.expr = FCALL_EXPR, .fcall.name = $1, .fcall.args = (Rhs *)$3->data, .fcall.argc = $3->count})); }
+ | VAR name type '=' rhs { NEW($$, ((Expr){.expr = ASSIGN, .assign.type = $3, .assign.name = $2, .assign.rhs = $5})); }
;
params : rhs {
@@ -127,7 +131,7 @@ params : rhs {
;
rhs : value { NEW($$, ((Rhs){.rhs = VALUE, .value = $1})); }
- | name '(' params ')' { NEW($$, ((Rhs){.rhs = FCALL_RHS, .fcall.name = $1->name})); }
+ | name '(' params ')' { NEW($$, ((Rhs){.rhs = FCALL_RHS, .fcall.name = $1})); }
| rhs '+' rhs { NEW($$, ((Rhs){.rhs = MATH, .math.op = ADD, .math.lhs = $1, .math.rhs = $3}));}
| rhs '-' rhs { NEW($$, ((Rhs){.rhs = MATH, .math.op = SUB, .math.lhs = $1, .math.rhs = $3}));}
| rhs '*' rhs { NEW($$, ((Rhs){.rhs = MATH, .math.op = MUL, .math.lhs = $1, .math.rhs = $3}));}
diff --git a/lex.c b/lex.c
@@ -126,12 +126,16 @@ struct Token lex_var() {
struct Token lex_name() {
struct Pos start = lex.pos;
+ int size = 0;
if (!isalpha(lex.input[0]))
llerror("Expected name!");
+
+ while (isalnum(lex.input[size]))
+ size++;
- struct Token t = {start, NAME, lex.input, lex.input + 1};
- move(1);
+ struct Token t = {start, NAME, lex.input, lex.input + size};
+ move(size);
switch (peek()) {
case '(': lex.f = &lex_obrace; break;
@@ -294,8 +298,8 @@ extern Func magic;
int main() {
lex = (struct Lexer){(struct Pos){0, 0},
- "func f() v { \n\
- var a v = 4; \n\
+ "func foo() i32 { \n\
+ var a i8 = 4; \n\
g((1 + 5) * 2); \n\
h(3, 5); \n\
}",