summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inc/coerce.h2
-rw-r--r--inc/exprtype.h21
-rw-r--r--inc/node.h3
-rw-r--r--samples/triangle.o74
-rw-r--r--src/coerce.c3
-rw-r--r--src/exprtype.c66
6 files changed, 97 insertions, 2 deletions
diff --git a/inc/coerce.h b/inc/coerce.h
index f15dbff..5595fb2 100644
--- a/inc/coerce.h
+++ b/inc/coerce.h
@@ -1,4 +1,4 @@
#pragma once
#include "type.h"
-void coerce(Node *expr, VecType *types);
+void coerce(Node *fn, VecType *types);
diff --git a/inc/exprtype.h b/inc/exprtype.h
new file mode 100644
index 0000000..aa54486
--- /dev/null
+++ b/inc/exprtype.h
@@ -0,0 +1,21 @@
+#pragma once
+
+typedef enum : u8 {
+ EXPRTYP_UNDEFINED,
+ EXPRTYP_VOID,
+ EXPRTYP_COMPTIMEINT,
+ EXPRTYP_PRIMARY,
+ EXPRTYP_COMPOUND,
+} ExprTypeTag;
+
+typedef struct ExprType ExprType;
+struct ExprType {
+ ExprTypeTag tag;
+ u8 id;
+ u64 ptr_depth;
+ u64 array_size;
+ ExprType *array_elems;
+};
+
+struct Node;
+ExprType exprtype(struct Node *expr);
diff --git a/inc/node.h b/inc/node.h
index 8bd3093..2231c68 100644
--- a/inc/node.h
+++ b/inc/node.h
@@ -1,5 +1,6 @@
#pragma once
#include "token.h"
+#include "exprtype.h"
typedef enum : u8 {
NOD_NONE,
@@ -47,6 +48,8 @@ struct Node {
Node *Rhs;
Node *rtype;
+ ExprType exprtype;
+
/* NOD_TYPE specifics */
u64 ptr_depth;
Node *array_size;
diff --git a/samples/triangle.o7 b/samples/triangle.o7
new file mode 100644
index 0000000..4589797
--- /dev/null
+++ b/samples/triangle.o7
@@ -0,0 +1,4 @@
+fn one -> i32 1i32;
+//fn one_crash -> i32 1u32;
+//fn two -> i32 2i32;
+//fn three -> i32 3i32;
diff --git a/src/coerce.c b/src/coerce.c
index 958c6af..2de1ca3 100644
--- a/src/coerce.c
+++ b/src/coerce.c
@@ -1,6 +1,7 @@
#include "coerce.h"
void
-coerce(Node *expr, VecType *types)
+coerce(Node *fn, VecType *types)
{
+ fn->Rhs->exprtype = exprtype(fn->Rhs);
}
diff --git a/src/exprtype.c b/src/exprtype.c
new file mode 100644
index 0000000..bb71dae
--- /dev/null
+++ b/src/exprtype.c
@@ -0,0 +1,66 @@
+#include "exprtype.h"
+#include "node.h"
+
+static ExprType _void(void);
+static ExprType _comptimeint(void);
+static ExprType _scop(Node *expr);
+static ExprType _numlit(Node *expr);
+
+ExprType
+exprtype(Node *expr)
+{
+ ExprType this = {
+ .tag = EXPRTYP_UNDEFINED,
+ .id = 0,
+ .ptr_depth = 0,
+ .array_size = 0,
+ .array_elems = nullptr,
+ };
+
+ switch (expr->type) {
+ case NOD_SCOP:
+ this = _scop(expr);
+ break;
+ case NOD_NUMLIT:
+ this = _numlit(expr);
+ break;
+ default:
+ panic("can't deduce the type of a %s expression",
+ nodtype_str(expr->type));
+ }
+
+ return this;
+}
+
+static ExprType
+_void(void)
+{
+ return (ExprType){
+ .tag = EXPRTYP_VOID
+ };
+}
+
+static ExprType
+_comptimeint(void)
+{
+ return (ExprType){
+ .tag = EXPRTYP_COMPTIMEINT
+ };
+}
+
+static ExprType
+_scop(Node *expr)
+{
+ for (auto instr = expr->Lhs; instr != nullptr; instr = instr->next) {
+ instr->exprtype = exprtype(instr);
+ if (instr->next == nullptr)
+ return instr->exprtype;
+ }
+ return _void();
+}
+
+static ExprType
+_numlit(Node *expr)
+{
+ return _comptimeint();
+}