summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-10-01 18:38:59 -0400
committerIan Moffett <ian@osmora.org>2025-10-01 18:38:59 -0400
commit3f246bba3ebb8c9df888a65ceca0ee96f597c051 (patch)
treea8f91f94cca13de5afd56257f6d9ac80d682c789
parenta3ac3a7b214991e3b555145dfc1348c58bf54349 (diff)
np: parse: Parse procedure signature
Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r--src/sys/include/np/ast.h15
-rw-r--r--src/sys/np/core/np_parse.c106
2 files changed, 106 insertions, 15 deletions
diff --git a/src/sys/include/np/ast.h b/src/sys/include/np/ast.h
index 9647f53..8e516d7 100644
--- a/src/sys/include/np/ast.h
+++ b/src/sys/include/np/ast.h
@@ -37,6 +37,21 @@
#include <string.h>
/*
+ * Valid program integer types
+ */
+typedef enum {
+ AST_BAD_TYPE,
+ AST_U8,
+ AST_U16,
+ AST_U32,
+ AST_U64,
+ AST_I8,
+ AST_I16,
+ AST_I32,
+ AST_I64
+} ast_itype_t;
+
+/*
* Represents an AST node
*
* @ident: Identifier
diff --git a/src/sys/np/core/np_parse.c b/src/sys/np/core/np_parse.c
index c560294..e72fa4e 100644
--- a/src/sys/np/core/np_parse.c
+++ b/src/sys/np/core/np_parse.c
@@ -125,6 +125,96 @@ parse_expect(struct np_work *work, char *cur, tt_t what, struct lex_token *tok)
}
/*
+ * Parse a type token
+ *
+ * @work: Current work
+ * @tok: Token result
+ *
+ * Returns the ast integer type on success, and AST_BAD_TYPE
+ * on failure.
+ */
+static ast_itype_t
+parse_type(struct np_work *work, struct lex_token *tok)
+{
+ tt_t tt;
+
+ if (work == NULL || tok == NULL) {
+ return AST_BAD_TYPE;
+ }
+
+ tt = parse_scan(work, tok);
+ switch (tt) {
+ /* Unsigned types */
+ case TT_U8: return AST_U8;
+ case TT_U16: return AST_U16;
+ case TT_U32: return AST_U32;
+ case TT_U64: return AST_U64;
+ }
+
+ return AST_BAD_TYPE;
+}
+
+/*
+ * Parse a procedure / function
+ *
+ * @work: Input work
+ * @tok: Current token
+ *
+ * Returns zero on success
+ */
+static int
+parse_proc(struct np_work *work, struct lex_token *tok)
+{
+ tt_t tt;
+
+ if (work == NULL || tok == NULL) {
+ return -EINVAL;
+ }
+
+ /* 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;
+ }
+
+ /* We need the first part of '->' */
+ tt = parse_expect(work, "<TT_RPAREN>", TT_MINUS, tok);
+ if (tok == TT_NONE) {
+ return -1;
+ }
+
+ /* Now we need to complete the '->' with '>' */
+ tt = parse_expect(work, "<TT_MINUS>", TT_GT, tok);
+ if (tok == TT_NONE) {
+ return -1;
+ }
+
+ /* And now the return type */
+ if (parse_type(work, tok) == AST_BAD_TYPE) {
+ pr_error(
+ "line %d: expected valid type, got %s\n",
+ work->line_no,
+ stoktab[tok->token]
+ );
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
* Parse a token
*
* @work: Input work
@@ -143,21 +233,7 @@ parse_token(struct np_work *work, struct lex_token *tok)
*/
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) {
+ if ((error = parse_proc(work, tok)) != 0) {
return -1;
}
break;