✨ update
This commit is contained in:
parent
da22dd03c0
commit
ec1985b3cc
|
@ -0,0 +1,21 @@
|
||||||
|
#include "ast_visitor.h"
|
||||||
|
|
||||||
|
void ast_visitor::visit_expr(expr* node) {
|
||||||
|
node->accept(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_visitor::visit_null_expr(null_expr* node) {
|
||||||
|
node->accept(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_visitor::visit_nil_expr(nil_expr* node) {
|
||||||
|
node->accept(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_visitor::visit_number_literal(number_literal* node) {
|
||||||
|
node->accept(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_visitor::visit_string_literal(string_literal* node) {
|
||||||
|
node->accept(this);
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "nasal_new_ast.h"
|
||||||
|
|
||||||
|
class ast_visitor {
|
||||||
|
public:
|
||||||
|
virtual void visit_expr(expr*);
|
||||||
|
virtual void visit_null_expr(null_expr*);
|
||||||
|
virtual void visit_nil_expr(nil_expr*);
|
||||||
|
virtual void visit_number_literal(number_literal*);
|
||||||
|
virtual void visit_string_literal(string_literal*);
|
||||||
|
};
|
|
@ -1,10 +1,27 @@
|
||||||
#include "nasal_new_ast.h"
|
#include "nasal_new_ast.h"
|
||||||
|
#include "ast_visitor.h"
|
||||||
|
|
||||||
bool expr::accept(ast_visitor* visitor) {
|
bool expr::accept(ast_visitor* visitor) {
|
||||||
visitor->visit_expr(this);
|
visitor->visit_expr(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_visitor::visit_expr(expr* node) {
|
bool null_expr::accept(ast_visitor* visitor) {
|
||||||
node->accept(this);
|
visitor->visit_null_expr(this);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool nil_expr::accept(ast_visitor* visitor) {
|
||||||
|
visitor->visit_nil_expr(this);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool number_literal::accept(ast_visitor* visitor) {
|
||||||
|
visitor->visit_number_literal(this);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool string_literal::accept(ast_visitor* visitor) {
|
||||||
|
visitor->visit_string_literal(this);
|
||||||
|
return true;
|
||||||
}
|
}
|
|
@ -5,8 +5,7 @@
|
||||||
|
|
||||||
enum class expr_type:u32 {
|
enum class expr_type:u32 {
|
||||||
ast_null=0, // null node
|
ast_null=0, // null node
|
||||||
ast_stmt,
|
ast_expr, // expression node
|
||||||
ast_expr,
|
|
||||||
ast_root, // mark the root node of ast
|
ast_root, // mark the root node of ast
|
||||||
ast_block, // expression block
|
ast_block, // expression block
|
||||||
ast_file, // used to store which file the sub-tree is on, only used in main block
|
ast_file, // used to store which file the sub-tree is on, only used in main block
|
||||||
|
@ -81,44 +80,98 @@ private:
|
||||||
span nd_loc;
|
span nd_loc;
|
||||||
expr_type nd_type;
|
expr_type nd_type;
|
||||||
public:
|
public:
|
||||||
expr(const span& location,expr_type node_type):
|
expr(const span& location, expr_type node_type):
|
||||||
nd_loc(location), nd_type(node_type) {}
|
nd_loc(location), nd_type(node_type) {}
|
||||||
~expr() = default;
|
~expr() = default;
|
||||||
virtual bool accept(ast_visitor*) = 0;
|
virtual bool accept(ast_visitor*) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class null_expr:public expr {};
|
class null_expr:public expr {
|
||||||
class nil_expr:public expr {};
|
|
||||||
class number_literal:public expr {};
|
|
||||||
class string_literal:public expr {};
|
|
||||||
class identifier:public expr {};
|
|
||||||
class bool_literal:public expr {};
|
|
||||||
class vector_expr:public expr {};
|
|
||||||
class hash_expr:public expr {};
|
|
||||||
class hash_pair:public expr {};
|
|
||||||
class function:public expr {};
|
|
||||||
class parameter:public expr {};
|
|
||||||
class ternary_operator:public expr {};
|
|
||||||
class binary_operator:public expr {};
|
|
||||||
class unary_operator:public expr {};
|
|
||||||
class call_expr:public expr {};
|
|
||||||
class call_hash:public expr {};
|
|
||||||
class call_vector:public expr {};
|
|
||||||
class call_function:public expr {};
|
|
||||||
class slice_vector:public expr {};
|
|
||||||
class definition:public expr {};
|
|
||||||
class multi_define:public expr {};
|
|
||||||
class while_expr:public expr {};
|
|
||||||
class for_expr:public expr {};
|
|
||||||
class foreach_expr:public expr {};
|
|
||||||
class forindex_expr:public expr {};
|
|
||||||
class condition_expr:public expr {};
|
|
||||||
class if_expr:public expr {};
|
|
||||||
class continue_expr:public expr {};
|
|
||||||
class break_expr:public expr {};
|
|
||||||
class return_expr:public expr {};
|
|
||||||
|
|
||||||
class ast_visitor {
|
|
||||||
public:
|
public:
|
||||||
virtual void visit_expr(expr*);
|
null_expr(const span& location):
|
||||||
|
expr(location, expr_type::ast_null) {}
|
||||||
|
~null_expr() = default;
|
||||||
|
virtual bool accept(ast_visitor*);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class nil_expr:public expr {
|
||||||
|
public:
|
||||||
|
nil_expr(const span& location):
|
||||||
|
expr(location, expr_type::ast_nil) {}
|
||||||
|
~nil_expr() = default;
|
||||||
|
virtual bool accept(ast_visitor*);
|
||||||
|
};
|
||||||
|
|
||||||
|
class number_literal:public expr {
|
||||||
|
private:
|
||||||
|
f64 number;
|
||||||
|
|
||||||
|
public:
|
||||||
|
number_literal(const span& location, const f64 num):
|
||||||
|
expr(location, expr_type::ast_num), number(num) {}
|
||||||
|
~number_literal() = default;
|
||||||
|
virtual bool accept(ast_visitor*);
|
||||||
|
};
|
||||||
|
|
||||||
|
class string_literal:public expr {
|
||||||
|
private:
|
||||||
|
string content;
|
||||||
|
|
||||||
|
public:
|
||||||
|
string_literal(const span& location, const string& str):
|
||||||
|
expr(location, expr_type::ast_str), content(str) {}
|
||||||
|
~string_literal() = default;
|
||||||
|
virtual bool accept(ast_visitor*);
|
||||||
|
};
|
||||||
|
|
||||||
|
class identifier:public expr {};
|
||||||
|
|
||||||
|
class bool_literal:public expr {};
|
||||||
|
|
||||||
|
class vector_expr:public expr {};
|
||||||
|
|
||||||
|
class hash_expr:public expr {};
|
||||||
|
|
||||||
|
class hash_pair:public expr {};
|
||||||
|
|
||||||
|
class function:public expr {};
|
||||||
|
|
||||||
|
class parameter:public expr {};
|
||||||
|
|
||||||
|
class ternary_operator:public expr {};
|
||||||
|
|
||||||
|
class binary_operator:public expr {};
|
||||||
|
|
||||||
|
class unary_operator:public expr {};
|
||||||
|
|
||||||
|
class call_expr:public expr {};
|
||||||
|
|
||||||
|
class call_hash:public expr {};
|
||||||
|
|
||||||
|
class call_vector:public expr {};
|
||||||
|
|
||||||
|
class call_function:public expr {};
|
||||||
|
|
||||||
|
class slice_vector:public expr {};
|
||||||
|
|
||||||
|
class definition:public expr {};
|
||||||
|
|
||||||
|
class multi_define:public expr {};
|
||||||
|
|
||||||
|
class while_expr:public expr {};
|
||||||
|
|
||||||
|
class for_expr:public expr {};
|
||||||
|
|
||||||
|
class foreach_expr:public expr {};
|
||||||
|
|
||||||
|
class forindex_expr:public expr {};
|
||||||
|
|
||||||
|
class condition_expr:public expr {};
|
||||||
|
|
||||||
|
class if_expr:public expr {};
|
||||||
|
|
||||||
|
class continue_expr:public expr {};
|
||||||
|
|
||||||
|
class break_expr:public expr {};
|
||||||
|
|
||||||
|
class return_expr:public expr {};
|
||||||
|
|
|
@ -18,6 +18,42 @@ const error& parse::compile(const lexer& lexer) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void parse::easter_egg() const {
|
||||||
|
std::clog
|
||||||
|
<< " _,,,_ \n"
|
||||||
|
<< " .' `'. \n"
|
||||||
|
<< " / ____ \\ Fucking Nasal Parser \n"
|
||||||
|
<< " | .-'_ _\\/ / \n"
|
||||||
|
<< " \\_/ a a| / \n"
|
||||||
|
<< " (,` \\ | .----. \n"
|
||||||
|
<< " | -' | /| '--. \n"
|
||||||
|
<< " \\ '= / || ]| `-. \n"
|
||||||
|
<< " /`-.__.' || ]| ::| \n"
|
||||||
|
<< " .-'`-.__ \\__ || ]| ::| \n"
|
||||||
|
<< " / `` `. || ]| ::| \n"
|
||||||
|
<< " _ | \\ \\ \\ \\| ]| .-' \n"
|
||||||
|
<< " / \\| \\ | \\ L.__ .--'( \n"
|
||||||
|
<< " | |\\ `. | \\ ,---|_ \\---------, \n"
|
||||||
|
<< " | | '. './\\ \\/ .--._|=- |_ /| \n"
|
||||||
|
<< " | \\ '. `'.'. /`\\/ .-' '. / | \n"
|
||||||
|
<< " | | `'. `;-:-;`)| |-./ | \n"
|
||||||
|
<< " | /_ `'--./_ ` )/'-------------')/) | \n"
|
||||||
|
<< " \\ | `\"\"\"\"----\"`\\//`\"\"`/,===..'`````````/ ( |\n"
|
||||||
|
<< " | | / `---` `===' / ) | \n"
|
||||||
|
<< " / \\ / / ( | \n"
|
||||||
|
<< " | '------. |'--------------------'| ) | \n"
|
||||||
|
<< " \\ `-| | / | \n"
|
||||||
|
<< " `--...,______| | ( | \n"
|
||||||
|
<< " | | | | ) ,| \n"
|
||||||
|
<< " | | | | ( /|| \n"
|
||||||
|
<< " | | | | )/ `\" \n"
|
||||||
|
<< " / \\ | | (/ \n"
|
||||||
|
<< " .' /I\\ '.| | /) \n"
|
||||||
|
<< " .-'_.'/ \\'. | | / \n"
|
||||||
|
<< " ``` `\"\"\"` `| .-------------------.|| \n"
|
||||||
|
<< " `\"` `\"` \n";
|
||||||
|
}
|
||||||
|
|
||||||
void parse::die(const span& loc, string info) {
|
void parse::die(const span& loc, string info) {
|
||||||
err.err("parse", loc, info);
|
err.err("parse", loc, info);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,41 +8,6 @@
|
||||||
#include "nasal_ast.h"
|
#include "nasal_ast.h"
|
||||||
#include "nasal_err.h"
|
#include "nasal_err.h"
|
||||||
|
|
||||||
/*
|
|
||||||
_,,,_
|
|
||||||
.' `'.
|
|
||||||
/ ____ \ Fucking Nasal Parser
|
|
||||||
| .-'_ _\/ /
|
|
||||||
\_/ a a| /
|
|
||||||
(,` \ | .----.
|
|
||||||
| -' | /| '--.
|
|
||||||
\ '= / || ]| `-.
|
|
||||||
/`-.__.' || ]| ::|
|
|
||||||
.-'`-.__ \__ || ]| ::|
|
|
||||||
/ `` `. || ]| ::|
|
|
||||||
_ | \ \ \ \| ]| .-'
|
|
||||||
/ \| \ | \ L.__ .--'(
|
|
||||||
| |\ `. | \ ,---|_ \---------,
|
|
||||||
| | '. './\ \/ .--._|=- |_ /|
|
|
||||||
| \ '. `'.'. /`\/ .-' '. / |
|
|
||||||
| | `'. `;-:-;`)| |-./ |
|
|
||||||
| /_ `'--./_ ` )/'-------------')/) |
|
|
||||||
\ | `""""----"`\//`""`/,===..'`````````/ ( |
|
|
||||||
| | / `---` `===' / ) |
|
|
||||||
/ \ / / ( |
|
|
||||||
| '------. |'--------------------'| ) |
|
|
||||||
\ `-| | / |
|
|
||||||
`--...,______| | ( |
|
|
||||||
| | | | ) ,|
|
|
||||||
| | | | ( /||
|
|
||||||
| | | | )/ `"
|
|
||||||
/ \ | | (/
|
|
||||||
.' /I\ '.| | /)
|
|
||||||
.-'_.'/ \'. | | /
|
|
||||||
``` `"""` `| .-------------------.||
|
|
||||||
`"` `"`
|
|
||||||
*/
|
|
||||||
|
|
||||||
class parse {
|
class parse {
|
||||||
|
|
||||||
#define thisspan (toks[ptr].loc)
|
#define thisspan (toks[ptr].loc)
|
||||||
|
@ -181,4 +146,5 @@ public:
|
||||||
root({0, 0, 0, 0, ""}, ast_root),
|
root({0, 0, 0, 0, ""}, ast_root),
|
||||||
err(e) {}
|
err(e) {}
|
||||||
const error& compile(const lexer&);
|
const error& compile(const lexer&);
|
||||||
|
void easter_egg() const;
|
||||||
};
|
};
|
7
makefile
7
makefile
|
@ -18,8 +18,9 @@ SRC=\
|
||||||
|
|
||||||
STD=c++14
|
STD=c++14
|
||||||
|
|
||||||
nasal:$(SRC) nasal_new_ast.o nasal_new_parse.o
|
nasal:$(SRC) nasal_new_ast.o nasal_new_parse.o ast_visitor.o
|
||||||
$(CXX) -std=$(STD) -O3 main.cpp -o nasal -fno-exceptions -ldl -Wshadow -Wall
|
$(CXX) -std=$(STD) -O3 main.cpp -o nasal -fno-exceptions -ldl -Wshadow -Wall
|
||||||
|
|
||||||
nasal.exe:$(SRC)
|
nasal.exe:$(SRC)
|
||||||
$(CXX) -std=$(STD) -O3 main.cpp -o nasal.exe -fno-exceptions -Wshadow -Wall -static
|
$(CXX) -std=$(STD) -O3 main.cpp -o nasal.exe -fno-exceptions -Wshadow -Wall -static
|
||||||
|
|
||||||
|
@ -29,8 +30,12 @@ nasal_new_ast.o: ast/nasal_new_ast.h ast/nasal_new_ast.cpp nasal.h
|
||||||
nasal_new_parse.o: ast/nasal_new_parse.h ast/nasal_new_parse.cpp nasal.h ast/nasal_new_ast.h
|
nasal_new_parse.o: ast/nasal_new_parse.h ast/nasal_new_parse.cpp nasal.h ast/nasal_new_ast.h
|
||||||
$(CXX) -std=$(STD) -c -O3 ast/nasal_new_ast.cpp -fno-exceptions -fPIC -o nasal_new_parse.o -I .
|
$(CXX) -std=$(STD) -c -O3 ast/nasal_new_ast.cpp -fno-exceptions -fPIC -o nasal_new_parse.o -I .
|
||||||
|
|
||||||
|
ast_visitor.o: ast/nasal_new_ast.h ast/ast_visitor.h ast/ast_visitor.cpp
|
||||||
|
$(CXX) -std=$(STD) -c -O3 ast/ast_visitor.cpp -fno-exceptions -fPIC -o ast_visitor.o -I .
|
||||||
|
|
||||||
stable-release:$(SRC)
|
stable-release:$(SRC)
|
||||||
$(CXX) -std=$(STD) -O2 main.cpp -o nasal -fno-exceptions -ldl -Wshadow -Wall
|
$(CXX) -std=$(STD) -O2 main.cpp -o nasal -fno-exceptions -ldl -Wshadow -Wall
|
||||||
|
|
||||||
stable-release-mingw:$(SRC)
|
stable-release-mingw:$(SRC)
|
||||||
$(CXX) -std=$(STD) -O2 main.cpp -o nasal.exe -fno-exceptions -Wshadow -Wall -static
|
$(CXX) -std=$(STD) -O2 main.cpp -o nasal.exe -fno-exceptions -Wshadow -Wall -static
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue