Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cac78e2

Browse files
Parsing update
Added if statement Added return statement Implemented script::findSymbol(id) function
1 parent 3147f94 commit cac78e2

File tree

8 files changed

+152
-54
lines changed

8 files changed

+152
-54
lines changed

‎api/engine.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "../compiler/parsing/node.h"
1111
#include "../include/engine.h"
1212

13+
1314
namespace olcl
1415
{
1516
namespace
@@ -61,15 +62,11 @@ namespace olcl
6162
dump_ast(*node.value.function.parameters, depth);
6263
std::cout << " -> ";
6364
dump_ast(*node.value.function.return_type, depth);
65+
std::cout << ' ';
6466

6567
if (node.value.function.code)
66-
{
67-
std::cout << " {\n";
68-
dump_ast(*node.value.function.code, depth + 1u);
69-
for (std::uint32_t i = 0; i < depth; ++i)
70-
std::cout << " ";
71-
std::cout << '}';
72-
}
68+
dump_ast(*node.value.function.code, depth);
69+
7370
if (escape_next) std::cout << ')';
7471
break;
7572
}
@@ -97,13 +94,19 @@ namespace olcl
9794
break;
9895

9996
case nt::t_block:
97+
std::cout << "{\n";
98+
10099
for (auto e : *node.block.code)
101100
{
102-
for (std::uint32_t i = 0; i < depth; ++i)
101+
for (std::uint32_t i = 0; i < depth + 1; ++i)
103102
std::cout << " ";
104-
dump_ast(*e, depth, false);
103+
dump_ast(*e, depth + 1u, false);
105104
std::cout << ";\n";
106105
}
106+
107+
for (std::uint32_t i = 0; i < depth; ++i)
108+
std::cout << " ";
109+
std::cout << '}';
107110
break;
108111

109112
case nt::t_list:
@@ -115,6 +118,25 @@ namespace olcl
115118
}
116119
if (escape_next) std::cout << ')';
117120
break;
121+
122+
case nt::t_stmt_return:
123+
std::cout << "return ";
124+
dump_ast(*node.stmt_return.expression, depth, false);
125+
break;
126+
127+
case nt::t_stmt_if:
128+
std::cout << "if ";
129+
dump_ast(*node.stmt_if.condition, depth);
130+
std::cout << " then ";
131+
dump_ast(*node.stmt_if.on_true, depth);
132+
133+
if (node.stmt_if.on_false)
134+
{
135+
std::cout << " else ";
136+
dump_ast(*node.stmt_if.on_false, depth);
137+
}
138+
139+
break;
118140
}
119141
}
120142
}

‎api/script.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66

77
namespace olcl
88
{
9+
namespace
10+
{
11+
symbol * val_to_symbol(node * type)
12+
{
13+
if (type->type != node_type::t_value)
14+
{
15+
// TODO: log error?
16+
return nullptr;
17+
}
18+
19+
return &type->value;
20+
}
21+
}
22+
923
script::script(olcl::node * ast_)
1024
: code(ast_)
1125
{
@@ -15,7 +29,12 @@ namespace olcl
1529

1630
symbol * script::findSymbol(std::string id) const
1731
{
18-
// TODO: Find that symbol
32+
for (auto b : *code->module.bindings)
33+
{
34+
if (*b->binding.name == id)
35+
return val_to_symbol(b->binding.value);
36+
}
37+
1938
return nullptr;
2039
}
2140
}

‎compiler/parsing/node.h

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <vector>
66
#include <map>
77

8+
#include "../../include/symbol.h"
89
#include "operators.h"
910

1011

@@ -25,49 +26,13 @@ namespace olcl
2526

2627
t_function_call,
2728

29+
t_stmt_if,
30+
t_stmt_return,
31+
2832
t_type,
2933
t_value,
3034
};
3135

32-
enum class symbol_type
33-
{
34-
t_variable,
35-
36-
t_string,
37-
t_character,
38-
t_number,
39-
40-
t_function,
41-
};
42-
43-
44-
struct node;
45-
struct context;
46-
47-
struct symbol
48-
{
49-
symbol_type type;
50-
union
51-
{
52-
// TODO: use special type
53-
std::string *variable;
54-
std::string *string;
55-
std::uint16_t character;
56-
// TODO: use special type
57-
std::string *number;
58-
59-
struct
60-
{
61-
context * context;
62-
63-
node * return_type;
64-
node * parameters;
65-
66-
node * code;
67-
} function;
68-
};
69-
};
70-
7136
struct context
7237
{
7338
context * parent;
@@ -131,6 +96,18 @@ namespace olcl
13196
} typeinfo;
13297

13398
olcl::symbol value;
99+
100+
struct
101+
{
102+
node * condition;
103+
node * on_true;
104+
node * on_false;
105+
} stmt_if;
106+
107+
struct
108+
{
109+
node * expression;
110+
} stmt_return;
134111
};
135112

136113
node() {}

‎compiler/parsing/operators.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "operators.h"
22

3+
34
#define ASSOC_LEFT false
45
#define ASSOC_RIGHT true
56

‎compiler/parsing/parser.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace olcl { namespace parsing {
1414
node * parse_expression(parser::state &state);
1515
node * parse_expression(node * left_operand, parser::state &state, int base_priority);
1616

17+
node * parse_statement(parser::state &state);
18+
1719
node * parse_binding(parser::state &state);
1820
node *parse_module(parser::state &state);
1921

@@ -125,7 +127,7 @@ namespace olcl { namespace parsing {
125127

126128
while (state.peek().sub_type != token_sub_type::t_brace_curly_right)
127129
{
128-
container.block.code->push_back(parse_unit(state));
130+
container.block.code->push_back(parse_statement(state));
129131

130132
if (state.is_error_occurred())
131133
return nullptr;
@@ -436,6 +438,41 @@ namespace olcl { namespace parsing {
436438
}
437439

438440
node * parse_statement(parser::state &state)
441+
{
442+
if (state.peek().type == token_type::t_identifier && state.peek().value == "if")
443+
{
444+
state.move();
445+
node * condition = parse_unit(state);
446+
node * on_true = parse_statement(state);
447+
node * on_false = nullptr;
448+
449+
if (state.peek().type == token_type::t_identifier && state.peek().value == "else")
450+
{
451+
state.move();
452+
on_false = parse_statement(state);
453+
}
454+
455+
node & container = *(new node);
456+
container.type = node_type::t_stmt_if;
457+
container.stmt_if.condition = condition;
458+
container.stmt_if.on_true = on_true;
459+
container.stmt_if.on_false = on_false;
460+
return &container;
461+
}
462+
463+
if (state.peek().type == token_type::t_identifier && state.peek().value == "return")
464+
{
465+
state.move();
466+
node & container = *(new node);
467+
container.type = node_type::t_stmt_return;
468+
container.stmt_return.expression = parse_unit(state);
469+
return &container;
470+
}
471+
472+
return parse_unit(state);
473+
}
474+
475+
node * parse_module_statement(parser::state &state)
439476
{
440477
if (state.peek().type == token_type::t_identifier)
441478
{
@@ -460,7 +497,7 @@ namespace olcl { namespace parsing {
460497

461498
while (!state.is_eof() && !state.is_error_occurred())
462499
{
463-
container.module.bindings->push_back(parse_statement(state));
500+
container.module.bindings->push_back(parse_module_statement(state));
464501

465502
if (state.is_error_occurred())
466503
return nullptr;

‎include/engine.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#pragma once
22

3+
#include "symbol.h"
34
#include "script.h"
45

56

67
namespace olcl
78
{
8-
struct symbol;
9-
109
class engine
1110
{
1211
public:

‎include/script.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
#include <string>
44

5+
#include "symbol.h"
6+
57

68
namespace olcl
79
{
810
struct node;
9-
struct symbol;
1011

1112
class script
1213
{

‎include/symbol.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
3+
namespace olcl
4+
{
5+
enum class symbol_type
6+
{
7+
t_variable,
8+
9+
t_string,
10+
t_character,
11+
t_number,
12+
13+
t_function,
14+
};
15+
16+
struct node;
17+
struct context;
18+
19+
struct symbol
20+
{
21+
symbol_type type;
22+
union
23+
{
24+
// TODO: use special type
25+
std::string *variable;
26+
std::string *string;
27+
std::uint16_t character;
28+
// TODO: use special type
29+
std::string *number;
30+
31+
struct
32+
{
33+
context * context;
34+
35+
node * return_type;
36+
node * parameters;
37+
38+
node * code;
39+
} function;
40+
};
41+
};
42+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /