diff options
author | Ian Moffett <ian@osmora.org> | 2025-10-02 20:06:06 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-10-02 20:06:06 -0400 |
commit | c904bc2b78045bd81cfc3603422c385cbf7adc75 (patch) | |
tree | c5a0dd4b35a8ee69512b18c12f774b3699a24d0a /src | |
parent | ee1e4b1e7cce34ff049c5ee48c8bbeef6c6e5596 (diff) |
kern: np: Add support for return statements
This commit introduces lexical, parsing and code generation support for
the return statements. As of now, one is only able to return unsigned integers
but this will be expanded upon in the future
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/sys/include/np/lex.h | 2 | ||||
-rw-r--r-- | src/sys/include/np/piir.h | 1 | ||||
-rw-r--r-- | src/sys/np/core/np_lex.c | 5 | ||||
-rw-r--r-- | src/sys/np/core/np_parse.c | 37 |
4 files changed, 44 insertions, 1 deletions
diff --git a/src/sys/include/np/lex.h b/src/sys/include/np/lex.h index ebd6f32..ba581b7 100644 --- a/src/sys/include/np/lex.h +++ b/src/sys/include/np/lex.h @@ -41,6 +41,7 @@ struct np_work; #define TOKEN_BEGIN "begin" #define TOKEN_PROC "proc" #define TOKEN_END "end" +#define TOKEN_RETURN "return" /* Types */ #define TOKEN_U8 "u8" @@ -93,6 +94,7 @@ typedef enum { TT_BEGIN, /* 'begin' */ TT_PROC, /* 'proc' */ TT_END, /* 'end' */ + TT_RETURN, /* 'return' */ } tt_t; typedef uint64_t tokval_t; diff --git a/src/sys/include/np/piir.h b/src/sys/include/np/piir.h index d108776..5625409 100644 --- a/src/sys/include/np/piir.h +++ b/src/sys/include/np/piir.h @@ -56,6 +56,7 @@ typedef int8_t md_byte_t; #define PIIR_LOAD_R32 0x03 /* Load 32-bit register */ #define PIIR_LOAD_R64 0x04 /* Load 64-bit register */ #define PIIR_RET_NIL 0x05 /* Return nothing */ +#define PIIR_RET_NUM 0x06 /* Return a number */ /* * Represents the PIIR virtual machine for storing diff --git a/src/sys/np/core/np_lex.c b/src/sys/np/core/np_lex.c index 389f1e2..222feba 100644 --- a/src/sys/np/core/np_lex.c +++ b/src/sys/np/core/np_lex.c @@ -144,6 +144,11 @@ static int lex_cmptok(char *tokstr, struct lex_token *res) { switch (*tokstr) { + case 'r': + if (strcmp(tokstr, TOKEN_RETURN) == 0) { + res->token = TT_RETURN; + } + return 0; case 'b': if (strcmp(tokstr, TOKEN_BEGIN) == 0) { res->token = TT_BEGIN; diff --git a/src/sys/np/core/np_parse.c b/src/sys/np/core/np_parse.c index 4119d8b..b7b854b 100644 --- a/src/sys/np/core/np_parse.c +++ b/src/sys/np/core/np_parse.c @@ -84,7 +84,8 @@ static const char *stoktab[] = { /* Keywords */ [TT_BEGIN] = "<TT_BEGIN>", [TT_PROC] = "<TT_PROC>", - [TT_END] = "<TT_END>" + [TT_END] = "<TT_END>", + [TT_RETURN] = "<TT_RETURN>" }; /* @@ -173,6 +174,35 @@ parse_type(struct np_work *work, struct lex_token *tok) } /* + * Parse a return statement + * + * @work: Input work + * @tok: Current token + * + * Returns zero on success + */ +static int +parse_return(struct np_work *work, struct lex_token *tok) +{ + tt_t tt; + + /* + * For now we'll only accept numbers as return values + * so we must see one after the return statement + */ + tt = parse_expect(work, "return", TT_NUMBER, tok); + if (tt == TT_NONE) { + return -1; + } + +#define PIIR_PUSH(BYTE) piir_push(work->piir_stack, (BYTE)) + PIIR_PUSH(PIIR_RET_NUM); + PIIR_PUSH(tok->val); +#undef PIIR_PUSH + return 0; +} + +/* * Parse a procedure / function * * @work: Input work @@ -311,6 +341,11 @@ parse_token(struct np_work *work, struct lex_token *tok) /* XXX: NOP for testing */ PIIR_PUSH(PIIR_NOP); break; + case TT_RETURN: + if ((error = parse_return(work, tok)) != 0) { + return -1; + } + break; } #undef PIIR_PUSH return 0; |