Author | Jakob Wakeling <[email protected]> |
Date | 2024-01-05 10:27:21 |
Commit | 37bab1883823d3f70471b53d3e0f545bca5644e4 |
Parent | b280badf2a766caf69094ea3c5b62c0463abb12f |
Remove the -> token from before return types
Diffstat
M | README.md | | | 6 | +++--- |
M | doc/g.ebnf | | | 2 | +- |
M | examples/hello.g | | | 2 | +- |
M | examples/io.g | | | 1 | + |
M | examples/loop.g | | | 0 | |
M | examples/main.g | | | 4 | ++-- |
M | src/lex.c | | | 52 | +++++++++++++++++++++++++++------------------------- |
M | src/lex.h | | | 2 | +- |
M | src/parse.c | | | 4 | +--- |
9 files changed, 37 insertions, 36 deletions
diff --git a/README.md b/README.md index 28d43b1..2c8da9c 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ print :: proc(s: string) { return; } -main :: proc() -> u8 { +main :: proc() u8 { print("Hello, World!\n"); return 0; } @@ -74,8 +74,9 @@ command. The second command will output an executable file, *a.out* by default. - [x] Implement type casting - [ ] Implement type casting to pointers and arrays - [ ] Improve type inference and implicit casting -- [ ] Implement the `type` type +- [ ] Implement the `type` type (?) - [ ] Implement the `error` type +- [ ] Implement the `string` type properly - [ ] Implement labels and `goto` - [x] Implement `if` and `else` - [x] Implement `for` @@ -83,7 +84,6 @@ command. The second command will output an executable file, *a.out* by default. - [ ] Implement `defer` - [ ] Implement `errdefer` (?) - [ ] Implement variadic procedure arguments -- [x] Implement first class strings - [x] Implement syscalls - [ ] Implement generics of some kind - [ ] Implement module definition diff --git a/doc/g.ebnf b/doc/g.ebnf index a881212..7d9397b 100644 --- a/doc/g.ebnf +++ b/doc/g.ebnf @@ -52,7 +52,7 @@ expr = iden | literal | iden, "<<=", expr | iden, ">>=" expr ; -expr_proc = "proc", "(", [ parm_list ], ")", [ "->", type ], stmt_compound ; +expr_proc = "proc", "(", [ parm_list ], ")", [ type ], stmt_compound ; (* Identifiers and Parameters *) parm = iden, ":", type ; diff --git a/examples/hello.g b/examples/hello.g index ecfebc2..fead61e 100644 --- a/examples/hello.g +++ b/examples/hello.g @@ -3,7 +3,7 @@ print :: proc(s: string) { return; } -main :: proc() -> u8 { +main :: proc() u8 { print("Hello, World!\n"); return 0; } diff --git a/examples/io.g b/examples/io.g index 9427d7a..6bbac3e 100644 --- a/examples/io.g +++ b/examples/io.g @@ -1,4 +1,4 @@ -main :: proc() -> sint { +main :: proc() sint { file : string = "g.txt"; body : string = "Output\n"; diff --git a/examples/loop.g b/examples/loop.g index 73c958d..c679822 100644 --- a/examples/loop.g +++ b/examples/loop.g @@ -1,4 +1,4 @@ -main :: proc() -> u8 { +main :: proc() u8 { for (i : u8 = 0; i < 6; i += 1) { c : u8 = i + 49; nl : u8 = '\n'; #syscall(uint(1), uint(1), &c, uint(1)); diff --git a/examples/main.g b/examples/main.g index 4ae7e76..f3796fe 100644 --- a/examples/main.g +++ b/examples/main.g @@ -1,7 +1,7 @@ -add3 :: proc(x: uint, y: uint, z: uint) -> uint { +add3 :: proc(x: uint, y: uint, z: uint) uint { return x + y + z; } -main :: proc() -> uint { +main :: proc() uint { return add3(1, 2, 3); } diff --git a/src/lex.c b/src/lex.c index fc0196f..cb75290 100644 --- a/src/lex.c +++ b/src/lex.c @@ -18,7 +18,7 @@ char *tok_ks[] = { "TK_IF", "TK_ELSE", "TK_FOR", "TK_BREAK", "TK_CONTINUE", "TK_LPAREN", "TK_RPAREN", "TK_LBRACK", "TK_RBRACK", "TK_LBRACE", "TK_RBRACE", - "TK_COLON", "TK_SCOLON", "TK_COMMA", "TK_PERIOD", "TK_RARROW", "TK_QMARK", "TK_HASH", + "TK_COLON", "TK_SCOLON", "TK_COMMA", "TK_PERIOD", "TK_QMARK", "TK_HASH", "TK_ADD", "TK_SUB", "TK_MUL", "TK_DIV", "TK_MOD", "TK_EQ", "TK_NE", "TK_LT", "TK_LE", "TK_GT", "TK_GE", @@ -44,12 +44,11 @@ lex lex_init(const char *file, char *src, uptr len) { lex_next(&l); return l; } -#define P (l->p) /* Pointer to the Current Character */ -#define Q (l->q) /* Pointer to EOF */ -#define C (l->p[0]) /* Current Character */ -#define LN (l->ln) /* Line Index */ -#define CL (l->cl) /* Column Index */ -#define T (l->t) /* Current Token */ +#define P (l->p) /* Pointer to the Current Character */ +#define Q (l->q) /* Pointer to EOF */ +#define LN (l->ln) /* Line Index */ +#define CL (l->cl) /* Column Index */ +#define T (l->t) /* Current Token */ /* Return the current token. */ tok lex_peek(lex *l) { return T; } @@ -60,7 +59,7 @@ tok lex_next(lex *l) { tok t = T; T = (tok){ 0 }; /* Skip null characters and whitespace */ - skip:; for (; P != Q && (!C || is_space(C)); P += 1) switch (C) { + skip:; for (; P != Q && (!P[0] || is_space(P[0])); P += 1) switch (P[0]) { case '\0': { note(l->n, l->ln, l->cl, 1, "Null character ignored"); } break; case '\n': { LN += 1; CL = 0; } break; default: { CL += 1; } break; @@ -70,13 +69,13 @@ tok lex_next(lex *l) { if (P == Q) { T = (tok){ TK_EOF, LN, CL, 0 }; return t; } /* Skip single-line and (potentially nested) multi-line comments */ - if (C == '/') switch (P[1]) { - case '/': { for (P += 2; P != Q && C != '\n'; P += 1); } goto skip; + if (P[0] == '/') switch (P[1]) { + case '/': { for (P += 2; P != Q && P[0] != '\n'; P += 1); } goto skip; case '*': { uptr d = 1; for (P += 2, CL += 2; P != Q && d; P += 1) { - if (C == '/' && P[1] == '*') { P += 2; CL += 2; d += 1; continue; } - if (C == '*' && P[1] == '/') { P += d == 1 ? 1 : 2; CL += 2; d -= 1; continue; } - if (C == '\n') { LN += 1; CL = 0; } else { CL += 1; } + if (P[0] == '/' && P[1] == '*') { P += 2; CL += 2; d += 1; continue; } + if (P[0] == '*' && P[1] == '/') { P += d == 1 ? 1 : 2; CL += 2; d -= 1; continue; } + if (P[0] == '\n') { LN += 1; CL = 0; } else { CL += 1; } } } goto skip; } @@ -84,10 +83,10 @@ tok lex_next(lex *l) { T.ln = LN; T.cl = CL; /* Handle identifiers and keywords */ - if (is_alpha(C) || C == '_') { + if (is_alpha(P[0]) || P[0] == '_') { char *s = P; uptr sl; - for (P += 1; is_alpha(C) || is_digit_dec(C) || C == '_'; P += 1); + for (P += 1; is_alpha(P[0]) || is_digit_dec(P[0]) || P[0] == '_'; P += 1); sl = P - s; CL += sl; T.h = syt_hash(s, sl); if (strncmp(s, "null", 4) == 0) { T.k = TK_NULL; } @@ -105,12 +104,12 @@ tok lex_next(lex *l) { } /* Handle number literals */ - else if (is_digit_dec(C)) { + else if (is_digit_dec(P[0])) { T.k = TK_INT; char *start = P; - for (P += 1; is_alpha(C) || is_digit_dec(C) || C == '.'; P += 1) { - if (C == '.') { T.k = TK_FLT; } - if (C == 'e' || C == 'E') { T.k = TK_FLT; P += P[1] == '+' || P[1] == '-'; } + for (P += 1; is_alpha(P[0]) || is_digit_dec(P[0]) || P[0] == '.'; P += 1) { + if (P[0] == '.') { T.k = TK_FLT; } + if (P[0] == 'e' || P[0] == 'E') { T.k = TK_FLT; P += P[1] == '+' || P[1] == '-'; } } uptr sl = P - start; CL += sl; @@ -156,7 +155,7 @@ tok lex_next(lex *l) { } /* Handle punctuators and operators */ - else switch (C) { + else switch (P[0]) { case '(': { T.k = TK_LPAREN; P += 1; CL += 1; } break; case ')': { T.k = TK_RPAREN; P += 1; CL += 1; } break; case '[': { T.k = TK_LBRACK; P += 1; CL += 1; } break; @@ -175,7 +174,6 @@ tok lex_next(lex *l) { } break; case '-': switch (P[1]) { default: { T.k = TK_SUB; P += 1; CL += 1; } break; - case '>': { T.k = TK_RARROW; P += 2; CL += 2; } break; case '=': { T.k = TK_AS_SUB; P += 2; CL += 2; } break; } break; case '*': switch (P[1]) { @@ -235,11 +233,11 @@ tok lex_next(lex *l) { /* Handle character and string literals */ case '\'': case '\"': { - char quote = C, *s = P += 1; CL += 1; register char *head = s; + char quote = P[0], *s = P += 1; CL += 1; register char *head = s; - for (; C != quote && C != '\n' && P != Q;) { + for (; P[0] != quote && P[0] != '\n' && P != Q;) { /* Non escape characters are not altered */ - if (C != '\\') { *head = C; P += 1; head += 1; } + if (P[0] != '\\') { *head = P[0]; P += 1; head += 1; } /* Escape characters are processed and re-written to head */ else switch (P[1]) { @@ -266,7 +264,7 @@ tok lex_next(lex *l) { } uptr sl = head - s; CL += sl; - if (C != quote) { note(l->n, T.ln, T.cl, 0, "Missing closing quote"); } + if (P[0] != quote) { note(l->n, T.ln, T.cl, 0, "Missing closing quote"); } else if (P != Q) { P += 1; CL += 1; } if (quote == '\'') { @@ -288,7 +286,7 @@ tok lex_next(lex *l) { } break; /* Handle unknown characters */ - default: { note(l->n, LN, CL, 1, "Unknown character: %X '%c'", C, C); P += 1; CL += 1; } break; + default: { note(l->n, LN, CL, 1, "Unknown character: %X '%c'", P[0], P[0]); P += 1; CL += 1; } break; } return t; diff --git a/src/lex.h b/src/lex.h index 6ca9044..98f8e56 100644 --- a/src/lex.h +++ b/src/lex.h @@ -14,7 +14,7 @@ typedef enum { TK_IF, TK_ELSE, TK_FOR, TK_BREAK, TK_CONTINUE, TK_LPAREN, TK_RPAREN, TK_LBRACK, TK_RBRACK, TK_LBRACE, TK_RBRACE, - TK_COLON, TK_SCOLON, TK_COMMA, TK_PERIOD, TK_RARROW, TK_QMARK, TK_HASH, + TK_COLON, TK_SCOLON, TK_COMMA, TK_PERIOD, TK_QMARK, TK_HASH, TK_ADD, TK_SUB, TK_MUL, TK_DIV, TK_MOD, TK_EQ, TK_NE, TK_LT, TK_LE, TK_GT, TK_GE, diff --git a/src/parse.c b/src/parse.c index 1f3a76d..394552f 100644 --- a/src/parse.c +++ b/src/parse.c @@ -434,9 +434,7 @@ static ast *parse_expr_proc(lex *l, syt *st) { lex_kind(l, TK_RPAREN); /* Parse optional procedure return type */ - if (T.k == TK_RARROW) { - lex_next(l); - + if (T.k == TK_ID) { ta_pair ta = parse_type(l, st); a->t = ta.t; /* TODO handle AST component */ }