Skip to content

Commit cfd4e42

Browse files
committed
WIP grammar
1 parent 9920ad3 commit cfd4e42

File tree

3 files changed

+415
-2
lines changed

3 files changed

+415
-2
lines changed

ast.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
/*
3+
Grammar:
4+
5+
6+
expr_base = integer | '(' expr ')'
7+
expr_pow = expr_bas _(** expr_pow)*
8+
expr_unary = ([~-] expr_unary)* | expr_base
9+
expr_mul = expr_unary ([* % / >> << &] expr_unary)*
10+
expr_add = expr_mul ([+-^] expr_mul)*
11+
expr = expr_add
12+
13+
14+
15+
stmt = 'return' expr
16+
| 'for' for_stmt
17+
| 'do' do_stmt
18+
| 'while' while_stmt
19+
| ASSIGNMENT
20+
21+
22+
23+
stmt_block = '{' stmt* '}'
24+
25+
26+
enum_item = NAME ('=' expr)? ','?
27+
enum_decl = '{' enum_item* '}'
28+
29+
field_item = NAME ':' type ','?
30+
aggregate_decl = NAME '{' field_item+ '}'
31+
32+
var_decl = NAME ':' type ('=' expr)?
33+
const_decl = NAME '=' expr
34+
35+
func_decl = NAME '(' func_parameter* ')' (':' type)? stmt_block
36+
37+
decl = 'enum' enum_decl
38+
| 'struct' aggregate_decl
39+
| 'union' aggregate_decl
40+
| 'var' var_decl
41+
| 'const' const_decl
42+
| 'func' func_decl
43+
*/
44+
45+
46+
typedef struct Decl Decl;
47+
typedef struct Expr Expr;
48+
typedef struct Typespec Typespec;
49+
typedef struct Stmt Stmt;
50+
51+
typedef enum {
52+
EXPR_NONE,
53+
EXPR_UNARY,
54+
EXPR_BINARY,
55+
EXPR_INT
56+
} ExprKind;
57+
58+
struct Expr {
59+
ExprKind kind;
60+
TokenKind op;
61+
int32_t val;
62+
Expr *left, *right;
63+
};
64+
65+
typedef struct {
66+
char *name;
67+
Expr *expr;
68+
} EnumItem;
69+
70+
struct Typespec {
71+
72+
};
73+
74+
typedef struct {
75+
char *name;
76+
Typespec typespec;
77+
} AggregateField;
78+
79+
typedef struct {
80+
AGGREGATE_NONE,
81+
AGGREGATE_STRUCT,
82+
AGGREGATE_UNION,
83+
} AggregateKind;
84+
85+
typedef struct {
86+
DECL_NONE,
87+
DECL_ENUM,
88+
DECL_AGGREGATE,
89+
DECL_VAR,
90+
DECL_CONST,
91+
DECL_FUNC,
92+
} DeclKind;
93+
94+
struct Decl {
95+
DeclKind kind;
96+
char *name;
97+
Expr *expr;
98+
union {
99+
EnumItem *enum_items;
100+
AggregateField *fields;
101+
};
102+
};
103+
104+
105+
Expr *parse_expr(void);
106+
Expr *expr_unary(TokenKind op, Expr *expr);
107+
Expr *expr_binary(TokenKind op, Expr *left, Expr *right);
108+
Expr *expr_int(int32_t val);
109+
110+

language.txt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
enum {
3+
A, B, C,
4+
}
5+
6+
enum {
7+
A = 13,
8+
B,
9+
C,
10+
}
11+
12+
struct Point {
13+
x: int,
14+
y: int,
15+
};
16+
17+
union Reg {
18+
i: int,
19+
f: float,
20+
};
21+
22+
var x: int;
23+
var y: int = 69;
24+
25+
func fib(int: n) : int {
26+
int result = 1;
27+
for (i := 1; i<n; ++i) {
28+
result += i;
29+
}
30+
return result;
31+
}
32+
33+
34+
35+
func some_shit() {
36+
x := 420;
37+
y := 69;
38+
39+
x += 1;
40+
x -= 2;
41+
42+
y *= 1;
43+
y /= 2;
44+
y %= 3;
45+
46+
y >>= 4;
47+
y <<= 5;
48+
49+
}

0 commit comments

Comments
 (0)