Author: mesyeti <mesyeti@mesyeti.uk>
stuff and errors
basic-boot/source/lexer.c | 2 basic-boot/source/parser.c | 101 +++++++++++++++++++++++++++++++++- basic-boot/source/parser.h | 1 basic-boot/source/preCompiler.h | 6 ++ basic-boot/source/state.h | 14 ++++ basic-boot/source/string.c | 3 basic-boot/test.bas | 6 -
diff --git a/basic-boot/source/lexer.c b/basic-boot/source/lexer.c
index 4a7d7cfb66bad3283a4d8110e80d10d85210d0d4..d5babcafa8a3d1116b530ab87527b38ea95dc83b 100644
--- a/basic-boot/source/lexer.c
+++ b/basic-boot/source/lexer.c
@@ -106,7 +106,6 @@ while (true) {
GET_CHAR(ch);
if (ch == '\n') {
- fseek(lexer->file, -1, SEEK_CUR);
return LEXER_NONE;
}
}
@@ -122,6 +121,7 @@ while (true) {
GET_CHAR_ERROR(ch)
if (IsSeparator(ch)) {
fseek(lexer->file, -1, SEEK_CUR);
+ if (ch == '\n') -- lexer->line;
break;
}
diff --git a/basic-boot/source/parser.c b/basic-boot/source/parser.c
index 904fc2179307fb02c938aba36b03b6acf92c1c16..15ab34963e2c9c42be747d340f024385d4538b88 100644
--- a/basic-boot/source/parser.c
+++ b/basic-boot/source/parser.c
@@ -247,7 +247,7 @@ static FuncDec ParseFuncDec(Parser* p) {
FuncDecParam* params = NULL;
size_t paramsNum = 0;
- bool sub = strcmp(p->tokens[p->i].contents, "sub") == 0;
+ bool sub = p->tokens[p->i].type == TOKEN_SUB;
Advance(p);
FuncDec ret;
@@ -297,7 +297,15 @@ ret.params = params;
ret.paramsNum = paramsNum;
Advance(p);
- Expect(p, TOKEN_LINE);
+ if (sub) {
+ Expect(p, TOKEN_LINE);
+ }
+ else {
+ ret.ret = SafeMalloc(sizeof(Node));
+ *ret.ret = ParseType(p);
+ Advance(p);
+ Expect(p, TOKEN_LINE);
+ }
return ret;
}
@@ -359,7 +367,7 @@
static Node ParseFuncCall(Parser* p) {
FuncCallNode ret;
ret.i = INFO(NODE_FUNC_CALL);
- ret.func = p->tokens[p->i].contents;
+ ret.func = NewString(p->tokens[p->i].contents);
Advance(p);
Node* params = NULL;
@@ -397,7 +405,7 @@
static Node ParseAssign(Parser* p) {
AssignNode ret;
ret.i = INFO(NODE_ASSIGN);
- ret.variable = p->tokens[p->i].contents;
+ ret.variable = NewString(p->tokens[p->i].contents);
Advance(p);
Expect(p, TOKEN_EQUAL);
@@ -483,7 +491,14 @@ Parser_PrintNode(node->ptr.inner);
break;
}
case NODE_FUNC_DEF: {
- printf("func %s(", node->funcDef.dec.name);
+ if (node->funcDef.dec.ret) {
+ printf("func ");
+ }
+ else {
+ printf("sub ");
+ }
+
+ printf("%s(", node->funcDef.dec.name);
for (size_t i = 0; i < node->funcDef.dec.paramsNum; ++ i) {
printf("%s as ", node->funcDef.dec.params[i].name);
@@ -523,6 +538,12 @@ }
puts("");
break;
}
+ case NODE_ASSIGN: {
+ printf("%s = ", node->assign.variable);
+ Parser_PrintNode(node->assign.rValue);
+ puts("");
+ break;
+ }
default: {
printf("unknown %d\n", node->i.type);
assert(0);
@@ -544,6 +565,76 @@ ret = SafeRealloc(ret, len * sizeof(Node));
ret[len - 1] = node;
}
+ for (size_t i = 0; i < p->tokenNum; ++ i) {
+ Lexer_FreeToken(&p->tokens[i]);
+ }
+ free(p->tokens);
+
*lenOut = len;
return ret;
}
+
+void Parser_FreeNode(Node* node) {
+ switch (node->i.type) {
+ case NODE_STRING: free(node->string.value); break;
+ case NODE_IDENTIFIER: free(node->ident.name); break;
+ case NODE_BINARY_OP: {
+ Parser_FreeNode(node->bin.left);
+ Parser_FreeNode(node->bin.right);
+ free(node->bin.left);
+ free(node->bin.right);
+ break;
+ }
+ case NODE_UNARY_OP: {
+ Parser_FreeNode(node->unary.operand);
+ free(node->unary.operand);
+ break;
+ }
+ case NODE_PTR: {
+ Parser_FreeNode(node->ptr.inner);
+ free(node->ptr.inner);
+ break;
+ }
+ case NODE_FUNC_DEF: {
+ if (node->funcDef.dec.ret) {
+ Parser_FreeNode(node->funcDef.dec.ret);
+ free(node->funcDef.dec.ret);
+ }
+
+ for (size_t i = 0; i < node->funcDef.dec.paramsNum; ++ i) {
+ Parser_FreeNode(node->funcDef.dec.params[i].type);
+ free(node->funcDef.dec.params[i].type);
+ }
+
+ free(node->funcDef.dec.name);
+
+ for (size_t i = 0; i < node->funcDef.bodyLen; ++ i) {
+ Parser_FreeNode(&node->funcDef.body[i]);
+ }
+ free(node->funcDef.body);
+ break;
+ }
+ case NODE_DIM: {
+ free(node->dim.name);
+ Parser_FreeNode(node->dim.varType);
+ free(node->dim.varType);
+ break;
+ }
+ case NODE_FUNC_CALL: {
+ free(node->funcCall.func);
+
+ for (size_t i = 0; i < node->funcCall.paramsNum; ++ i) {
+ Parser_FreeNode(&node->funcCall.params[i]);
+ }
+ free(node->funcCall.params);
+ break;
+ }
+ case NODE_ASSIGN: {
+ free(node->assign.variable);
+ Parser_FreeNode(node->assign.rValue);
+ free(node->assign.rValue);
+ break;
+ }
+ default: return;
+ }
+}
diff --git a/basic-boot/source/parser.h b/basic-boot/source/parser.h
index 531a4de3e1c355bb9ed18fc6a09f0668a3cb8f3a..c2e997bc58e5facc7a83355fe4a773f915abe012 100644
--- a/basic-boot/source/parser.h
+++ b/basic-boot/source/parser.h
@@ -120,5 +120,6 @@
Parser Parser_Init(FILE* file, const char* fileName);
void Parser_PrintNode(Node* node);
Node* Parser_Parse(Parser* p, size_t* lenOut);
+void Parser_FreeNode(Node* node);
#endif
diff --git a/basic-boot/source/preCompiler.h b/basic-boot/source/preCompiler.h
new file mode 100644
index 0000000000000000000000000000000000000000..a8301d1820668f79533fe14b72a3b45b15ab56f9
--- /dev/null
+++ b/basic-boot/source/preCompiler.h
@@ -0,0 +1,6 @@
+#ifndef N_PRECOMPILER_H
+#define N_PRECOMPILER_H
+
+#include "state.h"
+
+#endif
diff --git a/basic-boot/source/state.h b/basic-boot/source/state.h
new file mode 100644
index 0000000000000000000000000000000000000000..3615b4e6458066adecadecde1dcc84c8f82dda4c
--- /dev/null
+++ b/basic-boot/source/state.h
@@ -0,0 +1,14 @@
+#ifndef N_STATE_H
+#define N_STATE_H
+
+#include "common.h"
+
+typedef struct {
+ char* name;
+} Type;
+
+typedef struct {
+
+} State;
+
+#endif
diff --git a/basic-boot/source/string.c b/basic-boot/source/string.c
index 90f67c197bce783e0ceb85f96d4e2c820fcdad7b..7e42275935d2fa0e7044dae2263a0c206aadf2de 100644
--- a/basic-boot/source/string.c
+++ b/basic-boot/source/string.c
@@ -1,3 +1,4 @@
+#include <stdio.h>
#include <string.h>
#include "mem.h"
#include "string.h"
@@ -7,7 +8,7 @@ char* contents = SafeMalloc(1);
*contents = 0;
return (String) {
- SafeMalloc(1), 1
+ contents, 1
};
}
diff --git a/basic-boot/test.bas b/basic-boot/test.bas
index 3af3568083c1c16a9f9bf9fb6568dd7c197ef799..322b3b51d45b9deaa12f7bd1024131f47e44a2fb 100644
--- a/basic-boot/test.bas
+++ b/basic-boot/test.bas
@@ -2,12 +2,10 @@ 'NITRON
dim foo as int
-func manul(foo as int, bar as int)
+func manul(foo as int, bar as int) int
dim bar as u32
end
manul 1, (2 * 3) + -5 * 4
-func foo(bum bum bum bum)
-
-end
+foo = 3 + 4