summaryrefslogtreecommitdiff
path: root/src/sys/np
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-10-01 16:39:20 -0400
committerIan Moffett <ian@osmora.org>2025-10-01 16:40:12 -0400
commit8c8d31ed2c4127ba2965bd6a40ff0293418bc1e2 (patch)
treeeccdb84b6b8f5da2ea55c7e7ccbe7419ce4cc3d0 /src/sys/np
parentec3f0a41258099b4aa04ce236d062ffe0580f27d (diff)
np: parse: Add initial function parsing logic
Create basic parsing logic for functions, needs to be completed but provides great groundwork. We also added a better target to parse and we'll incrementally increase complexity as we advance Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src/sys/np')
-rw-r--r--src/sys/np/core/np_parse.c97
-rw-r--r--src/sys/np/sample/rev-0.np21
-rw-r--r--src/sys/np/sample/test-0.np16
3 files changed, 109 insertions, 25 deletions
diff --git a/src/sys/np/core/np_parse.c b/src/sys/np/core/np_parse.c
index 2b52cbe..12732cd 100644
--- a/src/sys/np/core/np_parse.c
+++ b/src/sys/np/core/np_parse.c
@@ -71,6 +71,97 @@ static const char *stoktab[] = {
[TT_END] = "<TT_END>"
};
+/*
+ * Scan the next token
+ *
+ * @work: Work input
+ * @token: Last token
+ *
+ * Returns the token type on success (TT_NONE on error)
+ */
+static inline tt_t
+parse_scan(struct np_work *work, struct lex_token *tok)
+{
+ if (lex_nom(work, tok) < 0) {
+ return TT_NONE;
+ }
+
+ return tok->token;
+}
+
+/*
+ * Scan and expect the next token to be a specific
+ * value
+ *
+ * @work: Work input
+ * @cur: The value before the value we expect
+ * @what: What we expect
+ * @tok: Last token
+ *
+ * Returns the token type on success (TT_NONE on error)
+ */
+static inline tt_t
+parse_expect(struct np_work *work, char *cur, tt_t what, struct lex_token *tok)
+{
+ tt_t tt = parse_scan(work, tok);
+
+ if (tt != what) {
+ pr_error(
+ "line %d: expected %s after '%s', got %s\n",
+ work->line_no,
+ stoktab[what],
+ cur,
+ stoktab[tok->token]
+ );
+
+ return TT_NONE;
+ }
+
+ return tt;
+}
+
+/*
+ * Parse a token
+ *
+ * @work: Input work
+ * @tok: Current token
+ */
+static int
+parse_token(struct np_work *work, struct lex_token *tok)
+{
+ tt_t tt;
+ int error;
+
+ /*
+ * XXX: wrapped in "[]" indicates optional
+ *
+ * TT_PROC => proc <TT_IDENT>(..., ...) [ -> <TYPE> ]
+ */
+ switch (tok->token) {
+ case TT_PROC:
+ /* We need the identifier */
+ tt = parse_expect(work, "proc", TT_IDENT, tok);
+ if (tt == TT_NONE) {
+ return -1;
+ }
+
+ /* Expect the left paren */
+ tt = parse_expect(work, "<TT_IDENT>", TT_LPAREN, tok);
+ if (tt == TT_NONE) {
+ return -1;
+ }
+
+ /* TODO: ARGS LATER */
+ tt = parse_expect(work, "<TT_LPAREN>", TT_RPAREN, tok);
+ if (tt == TT_NONE) {
+ return -1;
+ }
+ break;
+ }
+
+ return 0;
+}
+
int
parse_work(struct np_work *work)
{
@@ -94,10 +185,8 @@ parse_work(struct np_work *work)
return -1;
}
- /* Log the token */
- printf("tok.type: %s\n", stoktab[tok.token]);
- if (tok.token == TT_NUMBER) {
- printf("tok.val: %d\n", tok.val);
+ if (parse_token(work, &tok) < 0) {
+ return -1;
}
}
diff --git a/src/sys/np/sample/rev-0.np b/src/sys/np/sample/rev-0.np
deleted file mode 100644
index c3f781f..0000000
--- a/src/sys/np/sample/rev-0.np
+++ /dev/null
@@ -1,21 +0,0 @@
-;;
-;; Program entrypoint
-;;
-proc
-main(u8 argc, u8 **argv) -> i8
-begin
- u8 *vga = ptr(0x8B000)
-
- if argc < 2 begin
- return -1
- end
-
- ;; Draw '*' chars
- for [i=0 : i<8 : i += 2] begin
- vga[i + 0] = '*' ;; char '*'
- vga[i + 1] = 0x07 ;; red
- end
-
- return 0
-end
-
diff --git a/src/sys/np/sample/test-0.np b/src/sys/np/sample/test-0.np
new file mode 100644
index 0000000..b4ad2c1
--- /dev/null
+++ b/src/sys/np/sample/test-0.np
@@ -0,0 +1,16 @@
+proc
+main() -> i8
+begin
+ u8 buf[8]
+
+ if argc < 2 begin
+ return -1
+ end
+
+ for [i=0 : i<8 : i += 2] begin
+ buf[i] = 0x07
+ end
+
+ return 0
+end
+