Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit 1b980e5

Browse files
committed
feat: Add support for typing annotation
1 parent b5d0ca1 commit 1b980e5

21 files changed

+379
-124
lines changed

.makim.yaml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ env:
1919
:print_legend=1\
2020
:detect_leaks=1\
2121
"
22-
MESON_EXTRA: "-Db_coverage=true \
23-
-Doptimization=0 \
22+
MESON_EXTRA_DEBUG: "-Db_coverage=true \
23+
--optimization=0 \
24+
--debug \
2425
-Db_sanitize=address \
2526
"
2627
groups:
@@ -104,7 +105,7 @@ groups:
104105
- target: build.release
105106
args:
106107
build-type: "debug"
107-
meson-extra: {{ env.MESON_EXTRA }}
108+
meson-extra: {{ env.MESON_EXTRA_DEBUG }}
108109
clean: {{ args.clean }}
109110
asan-options: {{ env.SAN_OPTIONS_DEFAULT }}
110111
lsan-options: {{ env.SAN_OPTIONS_DEFAULT }}
@@ -120,7 +121,7 @@ groups:
120121
- target: build.release
121122
args:
122123
build-type: "debug"
123-
meson-extra: {{ env.MESON_EXTRA }} -Ddev=enabled
124+
meson-extra: {{ env.MESON_EXTRA_DEBUG }} -Ddev=enabled
124125
clean: {{ args.clean }}
125126
asan-options: {{ env.SAN_OPTIONS_DEFAULT }}
126127
lsan-options: {{ env.SAN_OPTIONS_DEFAULT }}
@@ -231,7 +232,7 @@ groups:
231232
232233
if [[ "{{ args.debug }}" == "True" ]]; then
233234
GDB="gdb --args"
234-
DEBUG_FLAGS="-g"
235+
DEBUG_FLAGS="-Og"
235236
fi
236237
237238
TEST_DIR_PATH="./tests"
@@ -261,7 +262,7 @@ groups:
261262
print_header "${test_name}"
262263
OBJECT_FILE="${TMP_DIR}/${test_name}.o"
263264
264-
${ARX} --output "${OBJECT_FILE}" --input "examples/${test_name}.arx"
265+
${ARX} --output "${OBJECT_FILE}" --input "examples/${test_name}.arx --build-lib"
265266
266267
set -x
267268
clang++ \

examples/average.arx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
function average(x y):
2-
(x + y) * 0.5;
1+
fn average(x: float, y: float) -> float:
2+
return (x + y) * 0.5;

examples/constant.arx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
function get_constant(x):
2-
x;
1+
fn get_constant(x: float) -> float:
2+
return x;

examples/fibonacci.arx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
function fib(x):
2-
if x < 3:
3-
1
1+
fn fib(x: float) -> float:
2+
if x <= 1:
3+
return x;
44
else:
5-
fib(x-1)+fib(x-2)
5+
return fib(x-1)+fib(x-2);

examples/print-star.arx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
extern putchard(char);
1+
extern putchard(char) -> void;
22

3-
function print_star(n):
3+
fn print_star(n: float) -> void:
44
for i = 1, i < n, 1.0 in
5-
putchard(42); # ascii 42 = '*'
5+
return putchard(42); # ascii 42 = '*'

examples/sum.arx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
function sum(a b):
2-
a + b;
1+
fn sum(a: float, b: float) -> float:
2+
return a + b;

meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ project('arx', 'cpp', 'c',
22
license : 'Apache-2.0',
33
version : '1.6.0', # semantic-release
44
default_options : [
5-
'warning_level=everything',
5+
#'warning_level=everything',
6+
'warning_level=1',
67
'cpp_std=c++20',
78
]
89
)

src/codegen/arx-llvm.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <string>
12

23
#include <llvm/IR/DIBuilder.h> // for DIBuilder
34
#include <llvm/IR/IRBuilder.h> // for IRBuilder
@@ -22,6 +23,26 @@ llvm::Type* ArxLLVM::FLOAT_TYPE;
2223
llvm::Type* ArxLLVM::DOUBLE_TYPE;
2324
llvm::Type* ArxLLVM::INT8_TYPE;
2425
llvm::Type* ArxLLVM::INT32_TYPE;
26+
llvm::Type* ArxLLVM::VOID_TYPE;
27+
28+
auto ArxLLVM::get_data_type(std::string type_name) -> llvm::Type* {
29+
if (type_name == "float") {
30+
return ArxLLVM::FLOAT_TYPE;
31+
} else if (type_name == "double") {
32+
return ArxLLVM::DOUBLE_TYPE;
33+
} else if (type_name == "int8") {
34+
return ArxLLVM::INT8_TYPE;
35+
} else if (type_name == "int32") {
36+
return ArxLLVM::INT32_TYPE;
37+
} else if (type_name == "char") {
38+
return ArxLLVM::INT8_TYPE;
39+
} else if (type_name == "void") {
40+
return ArxLLVM::VOID_TYPE;
41+
}
42+
43+
llvm::errs() << "[EE] type_name not valid.\n";
44+
return nullptr;
45+
}
2546

2647
/* Debug Information Data types */
2748
llvm::DIType* ArxLLVM::DI_FLOAT_TYPE;

src/codegen/ast-to-object.cpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ auto ASTToObjectVisitor::getFunction(std::string name) -> void {
9797
*/
9898
auto ASTToObjectVisitor::CreateEntryBlockAlloca(
9999
llvm::Function* fn, llvm::StringRef var_name) -> llvm::AllocaInst* {
100-
llvm::IRBuilder<> TmpB(&fn->getEntryBlock(), fn->getEntryBlock().begin());
101-
return TmpB.CreateAlloca(ArxLLVM::FLOAT_TYPE, nullptr, var_name);
100+
llvm::IRBuilder<> tmp_builder(
101+
&fn->getEntryBlock(), fn->getEntryBlock().begin());
102+
return tmp_builder.CreateAlloca(ArxLLVM::FLOAT_TYPE, nullptr, var_name);
102103
}
103104

104105
/**
@@ -165,7 +166,7 @@ auto ASTToObjectVisitor::visit(UnaryExprAST& expr) -> void {
165166
*
166167
*/
167168
auto ASTToObjectVisitor::visit(BinaryExprAST& expr) -> void {
168-
// Special case '=' because we don't want to emit the lhs as an
169+
// Special case '=' because we don't want to emit the lhs as an
169170
// expression.*/
170171
if (expr.op == '=') {
171172
// Assignment requires the lhs to be an identifier.
@@ -188,13 +189,13 @@ auto ASTToObjectVisitor::visit(BinaryExprAST& expr) -> void {
188189
};
189190

190191
// Look up the name.//
191-
llvm::Value* Variable = ArxLLVM::named_values[var_lhs->get_name()];
192-
if (!Variable) {
192+
llvm::Value* variable = ArxLLVM::named_values[LHSE->getName()];
193+
if (!variable) {
193194
this->result_val = LogErrorV("Unknown variable name");
194195
return;
195196
}
196197

197-
ArxLLVM::ir_builder->CreateStore(val, Variable);
198+
ArxLLVM::ir_builder->CreateStore(val, variable);
198199
this->result_val = val;
199200
}
200201

@@ -520,11 +521,32 @@ auto ASTToObjectVisitor::visit(VarExprAST& expr) -> void {
520521
*
521522
*/
522523
auto ASTToObjectVisitor::visit(PrototypeAST& expr) -> void {
523-
std::vector<llvm::Type*> args_type(expr.args.size(), ArxLLVM::FLOAT_TYPE);
524-
llvm::Type* return_type = ArxLLVM::get_data_type("float");
524+
// Make the function type: double(double,double) etc.
525+
std::vector<llvm::Type*> args;
526+
llvm::Type* arg_type;
527+
528+
for (auto& arg : expr.args) {
529+
arg_type = ArxLLVM::get_data_type(arg->type_name);
530+
if (arg_type != nullptr) {
531+
args.emplace_back(arg_type);
532+
} else {
533+
llvm::errs() << "ARX::GEN-OBJECT[ERROR]: PrototypeAST: "
534+
<< "Argument data type " << arg->type_name
535+
<< " not implemented yet.";
536+
}
537+
}
538+
539+
llvm::Type* return_type = ArxLLVM::get_data_type(expr.type_name);
540+
541+
if (return_type == nullptr) {
542+
llvm::errs() << "ARX::GEN-OBJECT[ERROR]: PrototypeAST: "
543+
<< "Argument data type " << expr.type_name
544+
<< " not implemented yet.";
545+
}
525546

526-
llvm::FunctionType* fn_type =
527-
llvm::FunctionType::get(return_type, args_type, false /* isVarArg */);
547+
llvm::FunctionType* fn_type = llvm::FunctionType::get(
548+
return_type, args, false /* isVarArg */
549+
);
528550

529551
llvm::Function* fn = llvm::Function::Create(
530552
fn_type,
@@ -794,7 +816,7 @@ auto compile_object(TreeAST& tree_ast) -> int {
794816
std::cout << "ARX[INFO]: " << compiler_cmd << std::endl;
795817
int compile_result = system(compiler_cmd.c_str());
796818

797-
// ArxFile::delete_file(main_cpp_path);
819+
ArxFile::delete_file(main_cpp_path);
798820

799821
if (compile_result != 0) {
800822
llvm::errs() << "failed to compile and link object file";

src/codegen/ast-to-stdout.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
int INDENT_SIZE = 2;
1010

11-
class ASTToOutputVisitor
12-
: public std::enable_shared_from_this<ASTToOutputVisitor>,
13-
public Visitor {
11+
class ASTToOutputVisitor : public Visitor {
1412
public:
1513
int indent = 0;
1614
std::string annotation = "";
@@ -53,12 +51,21 @@ void ASTToOutputVisitor::visit(FloatExprAST& expr) {
5351

5452
void ASTToOutputVisitor::visit(VariableExprAST& expr) {
5553
std::cout << this->indentation() << this->get_annotation()
56-
<< "(VariableExprAST " << expr.name << ")";
54+
<< "(VariableExprAST " << expr.name << ":" << expr.type_name
55+
<< ")";
5756
}
5857

5958
void ASTToOutputVisitor::visit(UnaryExprAST& expr) {
60-
std::cout << "(UnaryExprAST"
61-
<< ")" << std::endl;
59+
std::cout << this->indentation() << "(UnaryExprAST op_code:" << expr.op_code
60+
<< " operand:";
61+
62+
int cur_indent = this->indent;
63+
this->indent = 0;
64+
65+
expr.operand->accept(*this);
66+
std::cout << ")";
67+
68+
this->indent = cur_indent;
6269
}
6370

6471
void ASTToOutputVisitor::visit(BinaryExprAST& expr) {
@@ -197,29 +204,28 @@ void ASTToOutputVisitor::visit(VarExprAST& expr) {
197204

198205
void ASTToOutputVisitor::visit(PrototypeAST& expr) {
199206
// TODO: implement it
200-
std::cout << "(PrototypeAST " << expr.name << ")" << std::endl;
201-
}
202-
203-
void ASTToOutputVisitor::visit(FunctionAST& expr) {
204-
std::cout << this->indentation() << '(' << std::endl;
205-
this->indent += INDENT_SIZE;
206-
207-
// create the function and open the args section
208-
std::cout << this->indentation() << "Function " << expr.proto->name
207+
std::cout << "(PrototypeAST " << expr.name << ") -> " << expr.type_name
209208
<< " <ARGS> (" << std::endl;
210209
this->indent += INDENT_SIZE;
211210

212-
// std::cout << expr.proto->args.front();
213-
214-
for (const auto& node : expr.proto->args) {
211+
for (const auto& node : expr.args) {
215212
node->accept(*this);
216213
std::cout << ", " << std::endl;
217214
}
218215

219216
// close args section and open body section
220217
this->indent -= INDENT_SIZE;
221-
std::cout << this->indentation() << "), " << std::endl
222-
<< this->indentation() << "<BODY> (" << std::endl;
218+
std::cout << this->indentation() << "), " << std::endl;
219+
}
220+
221+
void ASTToOutputVisitor::visit(FunctionAST& expr) {
222+
std::cout << this->indentation() << '(' << std::endl;
223+
this->indent += INDENT_SIZE;
224+
225+
// create the function and open the args section
226+
std::cout << this->indentation() << "Function ";
227+
this->visit(*expr.proto);
228+
std::cout << this->indentation() << "<BODY> (" << std::endl;
223229

224230
this->indent += INDENT_SIZE;
225231
// TODO: body should be a vector of unique_ptr<Expr>

0 commit comments

Comments
 (0)