Skip to content

Commit

Permalink
prepare to use new ast
Browse files Browse the repository at this point in the history
  • Loading branch information
ValKmjolnir committed Jun 4, 2023
1 parent afc8c54 commit 9bf4eac
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
schedule:
- cron: "0 16 * * *"
push:
branches: [ master ]
branches: [ master,develop ]
pull_request:
branches: [ master ]
workflow_dispatch:
Expand Down
19 changes: 19 additions & 0 deletions ast/nasal_new_ast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "nasal_new_ast.h"

bool stmt::accept(ast_visitor* visitor) {
visitor->visit_stmt(this);
return true;
}

bool expr::accept(ast_visitor* visitor) {
visitor->visit_expr(this);
return true;
}

void ast_visitor::visit_stmt(stmt* node) {
node->accept(this);
}

void ast_visitor::visit_expr(expr* node) {
node->accept(this);
}
112 changes: 112 additions & 0 deletions ast/nasal_new_ast.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#pragma once

#include "nasal.h"
#include "nasal_err.h"

enum class ast_node_type:u32 {
ast_null=0, // null node
ast_stmt,
ast_expr,
ast_root, // mark the root node of ast
ast_block, // expression block
ast_file, // used to store which file the sub-tree is on, only used in main block
ast_nil, // nil keyword
ast_num, // number, basic value type
ast_str, // string, basic value type
ast_id, // identifier
ast_bool, // bools
ast_func, // func keyword
ast_hash, // hash, basic value type
ast_vec, // vector, basic value type
ast_pair, // pair of key and value in hashmap
ast_call, // mark a sub-tree of calling an identifier
ast_callh, // id.name
ast_callv, // id[index]
ast_callf, // id()
ast_subvec, // id[index:index]
ast_params, // mark a sub-tree of function parameters
ast_default, // default parameter
ast_dynamic, // dynamic parameter
ast_and, // and keyword
ast_or, // or keyword
ast_equal, // =
ast_addeq, // +=
ast_subeq, // -=
ast_multeq, // *=
ast_diveq, // /=
ast_lnkeq, // ~=
ast_btandeq, // &=
ast_btoreq, // |=
ast_btxoreq, // ^=
ast_cmpeq, // ==
ast_neq, // !=
ast_less, // <
ast_leq, // <=
ast_grt, // >
ast_geq, // >=
ast_add, // +
ast_sub, // -
ast_mult, // *
ast_div, // /
ast_link, // ~
ast_neg, // unary -
ast_lnot, // unary !
ast_bnot, // unary ~ bitwise not
ast_bitor, // bitwise or
ast_bitxor, // bitwise xor
ast_bitand, // bitwise and
ast_trino, // ?:
ast_for, // for keyword
ast_forindex, // forindex keyword
ast_foreach, // foreach keyword
ast_while, // while
ast_iter, // iterator, used in forindex/foreach
ast_cond, // mark a sub-tree of conditional expression
ast_if, // if keyword
ast_elsif, // elsif keyword
ast_else, // else keyword
ast_multi_id, // multi identifiers sub-tree
ast_tuple, // tuple, only used in multiple assignment
ast_def, // definition
ast_multi_assign,// multi assignment sub-tree
ast_continue, // continue keyword, only used in loop
ast_break, // break keyword, only used in loop
ast_ret // return keyword, only used in function block
};

class ast_visitor;

class ast_node {
private:
span nd_loc;
ast_node_type nd_type;
public:
ast_node(const span& location,ast_node_type node_type):
nd_loc(location), nd_type(node_type) {}
~ast_node() = default;
virtual bool accept(ast_visitor*) = 0;
};

class stmt:public ast_node {
public:
stmt(const span& location,ast_node_type node_type):
ast_node(location, node_type) {}
~stmt() = default;
virtual bool accept(ast_visitor*);
};

class expr:public ast_node {
private:

public:
expr(const span& location,ast_node_type node_type):
ast_node(location, node_type) {}
~expr() = default;
virtual bool accept(ast_visitor*);
};

class ast_visitor {
public:
virtual void visit_stmt(stmt*);
virtual void visit_expr(expr*);
};
5 changes: 4 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ SRC=\

STD=c++14

nasal:$(SRC)
nasal:$(SRC) nasal_new_ast.o
$(CXX) -std=$(STD) -O3 main.cpp -o nasal -fno-exceptions -ldl -Wshadow -Wall
nasal.exe:$(SRC)
$(CXX) -std=$(STD) -O3 main.cpp -o nasal.exe -fno-exceptions -Wshadow -Wall -static

nasal_new_ast.o: ast/nasal_new_ast.h ast/nasal_new_ast.cpp nasal.h
$(CXX) -std=$(STD) -c -O3 ast/nasal_new_ast.cpp -fno-exceptions -fPIC -o nasal_new_ast.o -I .

stable-release:$(SRC)
$(CXX) -std=$(STD) -O2 main.cpp -o nasal -fno-exceptions -ldl -Wshadow -Wall
stable-release-mingw:$(SRC)
Expand Down
Loading

0 comments on commit 9bf4eac

Please sign in to comment.