Skip to content

Commit

Permalink
split src/eval/eval.h to separate Ast from IR
Browse files Browse the repository at this point in the history
- create `src/eval/ast.h`, `src/eval/ir.h`, `src/eval/common.h`
- introduce a macro `MATCH_TYPED_UNION` for easier pattern matching of tagged unions
- move src/funcs/funcs.* to src/runtime/runtime.*
  • Loading branch information
ap29600 committed Sep 26, 2022
1 parent ccef622 commit e6c4737
Show file tree
Hide file tree
Showing 22 changed files with 1,961 additions and 302 deletions.
10 changes: 5 additions & 5 deletions codegen/generate_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def generate_implementation(name, info, left_t, right_t):
op = info['op'](left_t, right_t)
left_expr = f"(({left_t}*)left->data)[i]"
right_expr = f"(({right_t}*)right->data)[i]"
print(f"Eval_Node func_dyad_{name}_{left_t}_{right_t} (Eval_Node *left_node, Eval_Node *right_node) {{")
print( "\tassert(left_node->type == Node_Array);")
print( "\tassert(right_node->type == Node_Array);")
print(f"IR_Node func_dyad_{name}_{left_t}_{right_t} (IR_Node *left_node, IR_Node *right_node) {{")
print( "\tassert(left_node->type == IR_Type_Array);")
print( "\tassert(right_node->type == IR_Type_Array);")
print( "\tArray *left = borrow_array(left_node->as.array);")
print( "\tArray *right = borrow_array(right_node->as.array);")
print( "")
Expand All @@ -75,7 +75,7 @@ def generate_implementation(name, info, left_t, right_t):
print( "")
print( "\trelease_array(left);")
print( "\trelease_array(right);")
print( "\treturn (Eval_Node) {.type = Node_Array, .as.array = result};")
print( "\treturn (IR_Node) {.type = IR_Type_Array, .as.array = result};")
print( "}")
print( "")

Expand All @@ -94,7 +94,7 @@ def generate_func_binding(name, info, left_t, right_t):

if __name__ == '__main__':
bindings = []
print('#include "funcs.h"')
print('#include "runtime.h"')
print('')
for name, info in dyadic_funcs.items():
for left_t in types.keys():
Expand Down
37 changes: 37 additions & 0 deletions ir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef IR_H
#define IR_H

#include "src/eval/eval.h"

struct IR_Node;
struct Eval_Context;

typedef struct IR_Node (*func_t)(struct IR_Node *left, struct IR_Node *right);

typedef enum IR_Type {
IR_Type_None = 0,
IR_Type_Array,
IR_Type_Monad,
IR_Type_Dyad,
IR_Type_Assign,
IR_Type_Function,
} IR_Type;

typedef struct IR_Node {
IR_Type type;
Element_Type eval_type;
u64 ref_count;
union {
Array *array;
func_t function;
char identifier[16];
struct {
Node_Handle left;
Node_Handle right;
Node_Handle callee;
} args;
} as;
} IR_Node;


#endif // IR_H
10 changes: 10 additions & 0 deletions lib/builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ static inline i64 max(i64 a, i64 b) { return a >= b ? a : b; }

#endif

#define CONCAT(a, b) CONCAT2(a, b)

#define CONCAT2(a, b) a ## b

#define MATCH_TYPED_UNION(prefix, variant, bind_name, expression, field) \
break;case CONCAT(prefix, variant): for(variant bind_name = (expression).CONCAT(as_, variant), *__cond = (void*)0x1; __cond; __cond = NULL)

#define REF_MATCH_TYPED_UNION(prefix, variant, bind_name, expression, field) \
break;case CONCAT(prefix, variant): for(variant *bind_name = &(expression).CONCAT(as_, variant), *__cond = (void*)0x1; __cond; __cond = NULL)

#endif
2 changes: 1 addition & 1 deletion main.do
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LIB_NAMES=$( echo $LIB_ARCHIVES |\
sed 's/lib\/obj\/lib\([^/]*\).a/-l\1/' )

redo-ifchange $LIB_ARCHIVES
redo-ifchange src/main.c src/**/*.{c,h} src/funcs/generated.c
redo-ifchange src/main.c src/**/*.{c,h} src/runtime/generated.c

LINK="-lm -Llib/obj -Wl,--start-group $LIB_NAMES -Wl,--end-group"

Expand Down
55 changes: 55 additions & 0 deletions src/eval/ast.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef AST_H
#define AST_H

#include "lib/format/types.h"
#include "lib/string/types.h"

#include "common.h"

#include "src/array/array.h"

typedef enum Ast_Type {
Ast_Type_None = 0,
Ast_Type_Array_Ptr,
Ast_Type_Monadic,
Ast_Type_Dyadic,
Ast_Type_Assignment,
Ast_Type_Identifier,
} Ast_Type;

typedef Array * Array_Ptr;
typedef struct { Node_Handle func, right; } Monadic;
typedef struct { Node_Handle func, left, right; } Dyadic;
typedef struct { Node_Handle left, right; } Assignment;
typedef Short_String Identifier;

struct Ast_Node {
Ast_Type type;
union {
Array_Ptr as_Array_Ptr;
Monadic as_Monadic;
Dyadic as_Dyadic;
Assignment as_Assignment;
Identifier as_Identifier;
};
};

// variant: a possible type in struct `Ast_Node`'s anonymous union.
// bind_name: an identifier
// expression: a value of type `Ast_Node`
//
// introduces a case statement for the corresponding `Ast_Type` value,
// and declares a variable `bind_name` initialized to the specified
// union value of `expression`
#define MATCH_AST(variant, bind_name, expression) MATCH_TYPED_UNION(Ast_Type_, variant, bind_name, expression, type)

// variant: a possible type in struct `Ast_Node`'s anonymous union.
// bind_name: an identifier
// expression: a value of type `Ast_Node`
//
// introduces a case statement for the corresponding `Ast_Type` value,
// and declares a variable `bind_name` initialized to a pointer to the
// specified union value of `expression`
#define REF_MATCH_AST(variant, bind_name, expression) REF_MATCH_TYPED_UNION(Ast_Type_, bind_name, variant, expression, type)

#endif // AST_H
15 changes: 15 additions & 0 deletions src/eval/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef COMMON_H
#define COMMON_H

#include "lib/builtin.h"

typedef i32 Node_Handle;

typedef struct Ast_Node Ast_Node;
typedef struct IR_Node IR_Node;
typedef struct Eval_Context Eval_Context;
typedef struct Lookup_Scope Lookup_Scope;

typedef IR_Node (*func_t)(IR_Node *left, IR_Node *right);

#endif // COMMON_H
Loading

0 comments on commit e6c4737

Please sign in to comment.