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

Commit e124f26

Browse files
committed
apply typename for prototype and propagate changes to show-ast function
1 parent 13143bc commit e124f26

File tree

9 files changed

+77
-52
lines changed

9 files changed

+77
-52
lines changed

.github/workflows/main.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
main:
1111

1212
runs-on: ubuntu-latest
13-
timeout-minutes: 30
13+
timeout-minutes: 20
1414

1515
concurrency:
1616
group: ci-main-${{ github.ref }}

src/codegen/ast-to-stdout.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ void ASTToOutputVisitor::visit(FloatExprAST* expr) {
5151

5252
void ASTToOutputVisitor::visit(VariableExprAST* expr) {
5353
std::cout << this->indentation() << this->get_annotation()
54-
<< "(VariableExprAST " << expr->name << ")";
54+
<< "(VariableExprAST " << expr->name << ":" << expr->type_name
55+
<< ")";
5556
}
5657

5758
void ASTToOutputVisitor::visit(UnaryExprAST* expr) {
@@ -204,7 +205,7 @@ void ASTToOutputVisitor::visit(FunctionAST* expr) {
204205

205206
// create the function and open the args section
206207
std::cout << this->indentation() << "Function " << expr->proto->name
207-
<< " <ARGS> (" << std::endl;
208+
<< " -> " << expr->proto->type_name << " <ARGS> (" << std::endl;
208209
this->indent += INDENT_SIZE;
209210

210211
// std::cout << expr->proto->args.front();

src/lexer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
SourceLocation Lexer::cur_loc;
99
std::string Lexer::identifier_str =
10-
"<NOT DEFINED>"; // Filled in if tok_identifier
11-
double Lexer::NumVal; // Filled in if tok_number
10+
"<NOT DEFINED>"; // Filled in if tok_identifier
11+
double Lexer::num_float; // Filled in if tok_float
1212
SourceLocation Lexer::LexLoc;
1313
int Lexer::cur_tok = tok_not_initialized;
1414

@@ -30,7 +30,7 @@ auto Lexer::getTokName(int Tok) -> std::string {
3030
return "extern";
3131
case tok_identifier:
3232
return "identifier";
33-
case tok_number:
33+
case tok_float:
3434
return "number";
3535
case tok_if:
3636
return "if";
@@ -74,7 +74,7 @@ auto Lexer::getTokNameDisplay(int Tok) -> std::string {
7474
return "<extern>";
7575
case tok_identifier:
7676
return "<identifier>";
77-
case tok_number:
77+
case tok_float:
7878
return "<number>";
7979
case tok_if:
8080
return "<if>";
@@ -200,8 +200,8 @@ auto Lexer::gettok() -> int {
200200
LastChar = static_cast<char>(Lexer::advance());
201201
} while (isdigit(LastChar) || LastChar == '.');
202202

203-
Lexer::NumVal = strtod(NumStr.c_str(), nullptr);
204-
return tok_number;
203+
Lexer::num_float = strtod(NumStr.c_str(), nullptr);
204+
return tok_float;
205205
}
206206

207207
// Comment until end of line.

src/lexer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ enum Token {
1818

1919
// primary
2020
tok_identifier = -10,
21-
tok_number = -11,
21+
tok_float = -11,
2222

2323
// control
2424
tok_if = -20,
@@ -48,7 +48,7 @@ class Lexer {
4848
public:
4949
static SourceLocation cur_loc;
5050
static std::string identifier_str; // Filled in if tok_identifier
51-
static double NumVal; // Filled in if tok_number
51+
static double num_float; // Filled in if tok_float
5252
static int cur_tok;
5353
static SourceLocation LexLoc;
5454

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <glog/logging.h> // for InitGoogleLogging
1212
#include <stdlib.h> // for exit
1313
#include <CLI/CLI.hpp>
14+
#include <iostream>
1415
#include <string> // for string, allocator
1516
#include "codegen/ast-to-llvm-ir.h" // for compile_llvm_ir
1617
#include "codegen/ast-to-object.h" // for compile_object, open_shell_object
@@ -41,7 +42,6 @@ auto main_open_shell(__attribute__((unused)) int count) -> void {
4142
*
4243
*/
4344
auto main_show_version(__attribute__((unused)) int count) -> void {
44-
load_input_to_buffer();
4545
show_version();
4646
exit(0);
4747
}

src/parser.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,25 @@
1111
#include "error.h" // for LogError
1212
#include "lexer.h" // for Lexer, Lexer::cur_tok, Lexer::cur_loc, tok_iden...
1313

14+
static auto get_token_value(int tok) -> std::string {
15+
switch (tok) {
16+
case tok_identifier:
17+
return std::string("(") + Lexer::identifier_str + std::string(")");
18+
case tok_float:
19+
return std::string("(") + std::to_string(Lexer::num_float) +
20+
std::string(")");
21+
default:
22+
return std::string("");
23+
}
24+
}
25+
1426
#define LOG_PARSER_ERROR(tok_expected, tok_received, msg) \
1527
({ \
1628
LogParserError<PrototypeAST>( \
1729
(std::string("Expected ") + Lexer::getTokNameDisplay(tok_expected) + \
1830
std::string(", but received ") + \
19-
Lexer::getTokNameDisplay(tok_received) + std::string(". ") + msg) \
31+
Lexer::getTokNameDisplay(tok_received) + \
32+
get_token_value(tok_received) + std::string(". ") + msg) \
2033
.c_str(), \
2134
Lexer::cur_loc.line, \
2235
Lexer::cur_loc.col); \
@@ -108,7 +121,7 @@ auto Parser::GetTokPrecedence() -> int {
108121
* numberexpr ::= number
109122
*/
110123
std::unique_ptr<FloatExprAST> Parser::ParseFloatExpr() {
111-
auto Result = std::make_unique<FloatExprAST>(Lexer::NumVal);
124+
auto Result = std::make_unique<FloatExprAST>(Lexer::num_float);
112125
Lexer::getNextToken(); // consume the number
113126
return Result;
114127
}
@@ -147,7 +160,7 @@ std::unique_ptr<ExprAST> Parser::ParseIdentifierExpr() {
147160
Lexer::getNextToken(); // eat identifier.
148161

149162
if (Lexer::cur_tok != '(') { // Simple variable ref.
150-
return std::make_unique<VariableExprAST>(LitLoc, IdName, "float");
163+
return std::make_unique<VariableExprAST>(LitLoc, IdName, "REF");
151164
}
152165

153166
// Call. //
@@ -369,7 +382,7 @@ std::unique_ptr<ExprAST> Parser::ParsePrimary() {
369382
switch (Lexer::cur_tok) {
370383
case tok_identifier:
371384
return Parser::ParseIdentifierExpr();
372-
case tok_number:
385+
case tok_float:
373386
return static_cast<std::unique_ptr<ExprAST>>(ParseFloatExpr());
374387
case '(':
375388
return Parser::ParseParenExpr();
@@ -513,7 +526,7 @@ std::unique_ptr<PrototypeAST> Parser::ParseExternPrototype() {
513526
std::vector<VariableExprAST*> args;
514527
while (Lexer::getNextToken() == tok_identifier) {
515528
auto arg =
516-
new VariableExprAST(Lexer::cur_loc, Lexer::identifier_str, "float");
529+
new VariableExprAST(Lexer::cur_loc, Lexer::identifier_str, "REF");
517530
args.push_back(arg);
518531
}
519532
if (Lexer::cur_tok != ')') {
@@ -524,7 +537,7 @@ std::unique_ptr<PrototypeAST> Parser::ParseExternPrototype() {
524537
// success. //
525538
Lexer::getNextToken(); // eat ')'.
526539

527-
return std::make_unique<PrototypeAST>(FnLoc, FnName, args);
540+
return std::make_unique<PrototypeAST>(FnLoc, FnName, "REF", args);
528541
}
529542

530543
/**
@@ -592,8 +605,6 @@ std::unique_ptr<PrototypeAST> Parser::ParsePrototype() {
592605
}
593606

594607
// success. //
595-
Lexer::getNextToken(); // eat ")"
596-
597608
if (Lexer::getNextToken() != tok_arrow_right) {
598609
return LOG_PARSER_ERROR(
599610
tok_arrow_right,
@@ -613,13 +624,15 @@ std::unique_ptr<PrototypeAST> Parser::ParsePrototype() {
613624
// TODO: ret_type_annotation is not used yet
614625
ret_type_annotation = Lexer::identifier_str;
615626

616-
if (Lexer::cur_tok != ':') {
617-
return LOG_PARSER_ERROR(':', Lexer::cur_tok, std::string(""));
627+
if (Lexer::getNextToken() != ':') {
628+
return LOG_PARSER_ERROR(
629+
':',
630+
Lexer::cur_tok,
631+
std::string("Before starting the function body, use ':'."));
618632
}
619633

620-
Lexer::getNextToken(); // eat ':'.
621-
622-
return std::make_unique<PrototypeAST>(FnLoc, FnName, args);
634+
return std::make_unique<PrototypeAST>(
635+
FnLoc, FnName, ret_type_annotation, args);
623636
}
624637

625638
/**
@@ -650,7 +663,7 @@ std::unique_ptr<FunctionAST> Parser::ParseTopLevelExpr() {
650663
if (auto E = Parser::ParseExpression()) {
651664
// Make an anonymous proto.
652665
auto proto = std::make_unique<PrototypeAST>(
653-
FnLoc, "__anon_expr", std::vector<VariableExprAST*>());
666+
FnLoc, "__anon_expr", "ANONYMOUS", std::vector<VariableExprAST*>());
654667
return std::make_unique<FunctionAST>(std::move(proto), std::move(E));
655668
}
656669
return nullptr;

src/parser.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class VariableExprAST : public ExprAST {
123123
* @param name The variable name
124124
*/
125125
VariableExprAST(SourceLocation loc, std::string name, std::string type_name)
126-
: ExprAST(loc), name(std::move(name)) {
126+
: ExprAST(loc), name(std::move(name)), type_name(std::move(type_name)) {
127127
this->kind = ExprKind::VariableKind;
128128
}
129129

@@ -342,6 +342,7 @@ class PrototypeAST : public ExprAST {
342342
public:
343343
std::string name;
344344
std::vector<VariableExprAST*> args;
345+
std::string type_name;
345346
int line;
346347

347348
/**
@@ -350,8 +351,14 @@ class PrototypeAST : public ExprAST {
350351
* @param args The prototype arguments
351352
*/
352353
PrototypeAST(
353-
SourceLocation loc, std::string name, std::vector<VariableExprAST*> args)
354-
: name(std::move(name)), args(std::move(args)), line(loc.line) {
354+
SourceLocation loc,
355+
std::string name,
356+
std::string type_name,
357+
std::vector<VariableExprAST*> args)
358+
: name(std::move(name)),
359+
type_name(std::move(type_name)),
360+
args(std::move(args)),
361+
line(loc.line) {
355362
this->kind = ExprKind::PrototypeKind;
356363
}
357364

tests/test-lexer.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,30 @@ TEST(LexerTest, AdvanceTest) {
3232

3333
TEST(LexerTest, GetTokSimpleTest) {
3434
string_to_buffer((char*) "11");
35-
EXPECT_EQ(Lexer::gettok(), tok_number);
36-
EXPECT_EQ(Lexer::NumVal, 11);
35+
EXPECT_EQ(Lexer::gettok(), tok_float);
36+
EXPECT_EQ(Lexer::num_float, 11);
3737

3838
string_to_buffer((char*) "21");
39-
EXPECT_EQ(Lexer::gettok(), tok_number);
40-
EXPECT_EQ(Lexer::NumVal, 21);
39+
EXPECT_EQ(Lexer::gettok(), tok_float);
40+
EXPECT_EQ(Lexer::num_float, 21);
4141

4242
string_to_buffer((char*) "31");
43-
EXPECT_EQ(Lexer::gettok(), tok_number);
44-
EXPECT_EQ(Lexer::NumVal, 31);
43+
EXPECT_EQ(Lexer::gettok(), tok_float);
44+
EXPECT_EQ(Lexer::num_float, 31);
4545
}
4646

4747
TEST(LexerTest, GetNextTokenSimpleTest) {
4848
string_to_buffer((char*) "11");
49-
EXPECT_EQ(Lexer::getNextToken(), tok_number);
50-
EXPECT_EQ(Lexer::NumVal, 11);
49+
EXPECT_EQ(Lexer::getNextToken(), tok_float);
50+
EXPECT_EQ(Lexer::num_float, 11);
5151

5252
string_to_buffer((char*) "21");
53-
EXPECT_EQ(Lexer::getNextToken(), tok_number);
54-
EXPECT_EQ(Lexer::NumVal, 21);
53+
EXPECT_EQ(Lexer::getNextToken(), tok_float);
54+
EXPECT_EQ(Lexer::num_float, 21);
5555

5656
string_to_buffer((char*) "31");
57-
EXPECT_EQ(Lexer::getNextToken(), tok_number);
58-
EXPECT_EQ(Lexer::NumVal, 31);
57+
EXPECT_EQ(Lexer::getNextToken(), tok_float);
58+
EXPECT_EQ(Lexer::num_float, 31);
5959
}
6060

6161
TEST(LexerTest, GetTokTest) {
@@ -83,23 +83,23 @@ TEST(LexerTest, GetTokTest) {
8383
EXPECT_EQ(Lexer::gettok(), tok_if);
8484
EXPECT_EQ(Lexer::gettok(), tok_identifier);
8585
EXPECT_EQ(Lexer::gettok(), (int) '>');
86-
EXPECT_EQ(Lexer::gettok(), tok_number);
86+
EXPECT_EQ(Lexer::gettok(), tok_float);
8787
EXPECT_EQ(Lexer::gettok(), (int) ':');
8888
EXPECT_EQ(Lexer::gettok(), tok_return);
8989
EXPECT_EQ(Lexer::gettok(), tok_identifier);
9090
EXPECT_EQ(Lexer::gettok(), (int) '+');
91-
EXPECT_EQ(Lexer::gettok(), tok_number);
91+
EXPECT_EQ(Lexer::gettok(), tok_float);
9292
EXPECT_EQ(Lexer::gettok(), (int) ';');
9393
EXPECT_EQ(Lexer::gettok(), tok_else);
9494
EXPECT_EQ(Lexer::gettok(), (int) ':');
9595
EXPECT_EQ(Lexer::gettok(), tok_return);
9696
EXPECT_EQ(Lexer::gettok(), tok_identifier);
9797
EXPECT_EQ(Lexer::gettok(), (int) '*');
98-
EXPECT_EQ(Lexer::gettok(), tok_number);
98+
EXPECT_EQ(Lexer::gettok(), tok_float);
9999
EXPECT_EQ(Lexer::gettok(), (int) ';');
100100
EXPECT_EQ(Lexer::gettok(), tok_identifier);
101101
EXPECT_EQ(Lexer::gettok(), (int) '(');
102-
EXPECT_EQ(Lexer::gettok(), tok_number);
102+
EXPECT_EQ(Lexer::gettok(), tok_float);
103103
EXPECT_EQ(Lexer::gettok(), (int) ')');
104104
EXPECT_EQ(Lexer::gettok(), (int) ';');
105105
}

tests/test-parser.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
TEST(ParserTest, GetNextTokenTest) {
1212
/* Test gettok for main tokens */
1313
string_to_buffer((char*) R""""(
14-
fn math(x: float):
14+
fn math(x: float) -> float:
1515
if x > 10:
1616
return x + 1;
1717
else:
@@ -35,6 +35,10 @@ TEST(ParserTest, GetNextTokenTest) {
3535
Lexer::getNextToken();
3636
EXPECT_EQ(Lexer::cur_tok, (int) ')');
3737
Lexer::getNextToken();
38+
EXPECT_EQ(Lexer::cur_tok, tok_arrow_right);
39+
Lexer::getNextToken();
40+
EXPECT_EQ(Lexer::cur_tok, tok_identifier);
41+
Lexer::getNextToken();
3842
EXPECT_EQ(Lexer::cur_tok, (int) ':');
3943
Lexer::getNextToken();
4044
EXPECT_EQ(Lexer::cur_tok, tok_if);
@@ -43,7 +47,7 @@ TEST(ParserTest, GetNextTokenTest) {
4347
Lexer::getNextToken();
4448
EXPECT_EQ(Lexer::cur_tok, (int) '>');
4549
Lexer::getNextToken();
46-
EXPECT_EQ(Lexer::cur_tok, tok_number);
50+
EXPECT_EQ(Lexer::cur_tok, tok_float);
4751
Lexer::getNextToken();
4852
EXPECT_EQ(Lexer::cur_tok, (int) ':');
4953
Lexer::getNextToken();
@@ -53,7 +57,7 @@ TEST(ParserTest, GetNextTokenTest) {
5357
Lexer::getNextToken();
5458
EXPECT_EQ(Lexer::cur_tok, (int) '+');
5559
Lexer::getNextToken();
56-
EXPECT_EQ(Lexer::cur_tok, tok_number);
60+
EXPECT_EQ(Lexer::cur_tok, tok_float);
5761
Lexer::getNextToken();
5862
EXPECT_EQ(Lexer::cur_tok, (int) ';');
5963
Lexer::getNextToken();
@@ -67,15 +71,15 @@ TEST(ParserTest, GetNextTokenTest) {
6771
Lexer::getNextToken();
6872
EXPECT_EQ(Lexer::cur_tok, (int) '*');
6973
Lexer::getNextToken();
70-
EXPECT_EQ(Lexer::cur_tok, tok_number);
74+
EXPECT_EQ(Lexer::cur_tok, tok_float);
7175
Lexer::getNextToken();
7276
EXPECT_EQ(Lexer::cur_tok, (int) ';');
7377
Lexer::getNextToken();
7478
EXPECT_EQ(Lexer::cur_tok, tok_identifier);
7579
Lexer::getNextToken();
7680
EXPECT_EQ(Lexer::cur_tok, (int) '(');
7781
Lexer::getNextToken();
78-
EXPECT_EQ(Lexer::cur_tok, tok_number);
82+
EXPECT_EQ(Lexer::cur_tok, tok_float);
7983
Lexer::getNextToken();
8084
EXPECT_EQ(Lexer::cur_tok, (int) ')');
8185
Lexer::getNextToken();
@@ -101,7 +105,7 @@ TEST(ParserTest, ParseFloatExprTest) {
101105
string_to_buffer((char*) "1 2;");
102106

103107
tok = Lexer::getNextToken(); // update Lexer::cur_tok
104-
EXPECT_EQ(tok, tok_number);
108+
EXPECT_EQ(tok, tok_float);
105109
expr = Parser::ParseFloatExpr();
106110
EXPECT_NE(expr, nullptr);
107111
EXPECT_EQ(expr->Val, 1);
@@ -115,7 +119,7 @@ TEST(ParserTest, ParseFloatExprTest) {
115119
string_to_buffer((char*) "3");
116120

117121
tok = Lexer::getNextToken();
118-
EXPECT_EQ(tok, tok_number);
122+
EXPECT_EQ(tok, tok_float);
119123
expr = Parser::ParseFloatExpr();
120124
EXPECT_NE(expr, nullptr);
121125
EXPECT_EQ(expr->Val, 3);

0 commit comments

Comments
 (0)