Skip to content

Commit 35571d5

Browse files
authored
Merge pull request #106 from Quickfall/feat/better-syntax
[feat] Better Syntax
2 parents 0a3e7c1 + 1c2f11d commit 35571d5

66 files changed

Lines changed: 1133 additions & 208 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ out
55
out.*
66

77
*.qf
8-
*.qfmir
98
*.llvm
9+
*.air
1010

1111
!examples/**.qf

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/ast/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
55
pub mod ctx;
66
pub mod operators;
7+
pub mod ranges;
78
pub mod tree;
89
pub mod types;

compiler/ast/src/operators.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,23 @@ pub fn parse_compare_operator(
7272
_ => false,
7373
};
7474

75+
if eq {
76+
*ind += 1;
77+
}
78+
7579
let op = match tokens[*ind].tok_type {
7680
LexerTokenType::EqualSign => {
77-
tokens[*ind + 1].expects(LexerTokenType::EqualSign)?;
81+
if !eq {
82+
tokens[*ind + 1].expects(LexerTokenType::EqualSign)?;
83+
}
7884

7985
ComparingOperator::Equal
8086
}
8187

8288
LexerTokenType::ExclamationMark => {
83-
tokens[*ind + 1].expects(LexerTokenType::EqualSign)?;
89+
if !eq {
90+
tokens[*ind + 1].expects(LexerTokenType::EqualSign)?;
91+
}
8492

8593
ComparingOperator::NotEqual
8694
}
@@ -106,5 +114,7 @@ pub fn parse_compare_operator(
106114
}
107115
};
108116

117+
*ind += 1;
118+
109119
return Ok(op);
110120
}

compiler/ast/src/ranges.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use crate::tree::ASTTreeNode;
2+
3+
#[derive(Debug, PartialEq, Clone)]
4+
pub struct ASTRange {
5+
pub min: Box<ASTTreeNode>,
6+
pub max: Box<ASTTreeNode>,
7+
}

compiler/ast/src/tree.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use diagnostics::{
1515
diagnostic::{Diagnostic, Span, SpanKind, SpanPosition},
1616
};
1717

18-
use crate::types::ASTType;
18+
use crate::{ranges::ASTRange, types::ASTType};
1919

2020
#[derive(Debug, PartialEq, Clone)]
2121
pub struct FunctionDeclarationArgument {
@@ -78,7 +78,13 @@ pub enum ASTTreeNodeKind {
7878

7979
VariableReference(HashedString),
8080

81-
PointerGrab(Box<ASTTreeNode>),
81+
Dereference(Box<ASTTreeNode>),
82+
83+
DereferenceModify {
84+
pointer: Box<ASTTreeNode>,
85+
val: Box<ASTTreeNode>,
86+
},
87+
8288
ReferenceGrab(Box<ASTTreeNode>),
8389

8490
StructInitializer {
@@ -166,13 +172,20 @@ pub enum ASTTreeNodeKind {
166172
cond: Box<ASTTreeNode>,
167173
body: Vec<Box<ASTTreeNode>>,
168174
},
175+
169176
ForBlock {
170177
initial_state: Box<ASTTreeNode>,
171178
cond: Box<ASTTreeNode>,
172179
increment: Box<ASTTreeNode>,
173180
body: Vec<Box<ASTTreeNode>>,
174181
},
175182

183+
RangedForBlock {
184+
var: Box<ASTTreeNode>,
185+
range: ASTRange,
186+
body: Vec<Box<ASTTreeNode>>,
187+
},
188+
176189
FunctionCall {
177190
func: HashedString,
178191
args: Vec<Box<ASTTreeNode>>,
@@ -185,7 +198,7 @@ pub enum ASTTreeNodeKind {
185198
requires_this: bool,
186199
},
187200

188-
ShadowFunctionDeclaration {
201+
ExternFunctionDeclaration {
189202
func_name: HashedString,
190203
args: Vec<FunctionDeclarationArgument>,
191204
return_type: Option<ASTType>,
@@ -222,7 +235,7 @@ impl ASTTreeNodeKind {
222235
ASTTreeNodeKind::FunctionDeclaration { .. }
223236
| ASTTreeNodeKind::EnumDeclaration { .. }
224237
| ASTTreeNodeKind::StaticVariableDeclaration { .. }
225-
| ASTTreeNodeKind::ShadowFunctionDeclaration { .. }
238+
| ASTTreeNodeKind::ExternFunctionDeclaration { .. }
226239
| ASTTreeNodeKind::StructLayoutDeclaration { .. }
227240
);
228241
}
@@ -239,7 +252,7 @@ impl ASTTreeNodeKind {
239252
return Some(HashedString::new(func_name.val.to_string()));
240253
}
241254

242-
ASTTreeNodeKind::ShadowFunctionDeclaration {
255+
ASTTreeNodeKind::ExternFunctionDeclaration {
243256
func_name,
244257
args: _,
245258
return_type: _,
@@ -349,7 +362,8 @@ impl Display for ASTTreeNodeKind {
349362
Self::BooleanBasedConditionMember { .. } => "boolean condition",
350363
Self::MathResult { .. } => "math operation",
351364
Self::VariableReference(_) => "variable reference",
352-
Self::PointerGrab(_) => "pointer grabbing",
365+
Self::DereferenceModify { .. } => "modifying dereference",
366+
Self::Dereference(_) => "dereference",
353367
Self::ReferenceGrab(_) => "reference",
354368
Self::StructInitializer { .. } => "struct value initializer",
355369
Self::ArrayVariableInitializerValue { .. }
@@ -364,10 +378,10 @@ impl Display for ASTTreeNodeKind {
364378
Self::ReturnStatement { .. } => "return statement",
365379
Self::StaticVariableDeclaration { .. } => "static variable declaration",
366380
Self::WhileBlock { .. } => "while block",
367-
Self::ForBlock { .. } => "for block",
381+
Self::ForBlock { .. } | Self::RangedForBlock { .. } => "for block",
368382
Self::FunctionCall { .. } => "function call",
369383
Self::FunctionDeclaration { .. } => "function declaration",
370-
Self::ShadowFunctionDeclaration { .. } => "shadow function declaration",
384+
Self::ExternFunctionDeclaration { .. } => "extern function declaration",
371385
Self::StructLRFunction { .. } => "struct LRU function usage",
372386
Self::StructLRVariable { .. } => "struct LRU variable usage",
373387
Self::StructLayoutDeclaration { .. } => "struct / layout declaration",

compiler/ast_parser/src/control/for_loop.rs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use lexer::token::{LexerToken, LexerTokenType};
44
use ast::tree::{ASTTreeNode, ASTTreeNodeKind};
55

66
use crate::{
7-
functions::parse_node_body, parser::parse_ast_node_in_body, value::parse_ast_value,
8-
variables::decl::parse_variable_declaration,
7+
functions::parse_node_body, parser::parse_ast_node_in_body, ranges::parse_value_range,
8+
value::parse_ast_value, variables::decl::parse_variable_declaration,
99
};
1010

1111
pub fn parse_for_loop(
@@ -14,11 +14,18 @@ pub fn parse_for_loop(
1414
) -> DiagnosticResult<Box<ASTTreeNode>> {
1515
let start = tokens[*ind].pos.clone();
1616

17+
if tokens[*ind + 1].tok_type != LexerTokenType::ParenOpen {
18+
return parse_for_ranged_loop(tokens, ind);
19+
}
20+
1721
*ind += 1;
1822

1923
tokens[*ind].expects(LexerTokenType::ParenOpen)?;
24+
*ind += 1;
25+
26+
tokens[*ind].expects(LexerTokenType::Var)?;
2027

21-
let initial = parse_variable_declaration(tokens, ind)?;
28+
let initial = parse_variable_declaration(tokens, ind, true)?;
2229

2330
tokens[*ind].expects(LexerTokenType::Comma)?;
2431

@@ -50,3 +57,32 @@ pub fn parse_for_loop(
5057
end,
5158
)));
5259
}
60+
61+
pub fn parse_for_ranged_loop(
62+
tokens: &Vec<LexerToken>,
63+
ind: &mut usize,
64+
) -> DiagnosticResult<Box<ASTTreeNode>> {
65+
let start = tokens[*ind].pos.clone();
66+
67+
let var = parse_variable_declaration(tokens, ind, false)?;
68+
69+
tokens[*ind].expects(LexerTokenType::EqualSign)?;
70+
*ind += 1;
71+
72+
tokens[*ind].expects(LexerTokenType::AngelBracketClose)?;
73+
*ind += 1;
74+
75+
let range = parse_value_range(tokens, ind)?;
76+
77+
tokens[*ind].expects(LexerTokenType::BracketOpen)?;
78+
79+
let body = parse_node_body(tokens, ind)?;
80+
81+
let end: compiler_utils::Position = tokens[*ind - 1].get_end_pos();
82+
83+
Ok(Box::new(ASTTreeNode::new(
84+
ASTTreeNodeKind::RangedForBlock { var, range, body },
85+
start,
86+
end,
87+
)))
88+
}

compiler/ast_parser/src/functions/mod.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ pub mod arguments;
1717
pub mod returns;
1818
pub mod shadow;
1919

20+
pub fn parse_function_return_type(
21+
tokens: &Vec<LexerToken>,
22+
ind: &mut usize,
23+
) -> DiagnosticResult<Option<ASTType>> {
24+
if tokens[*ind].tok_type == LexerTokenType::Minus {
25+
*ind += 1;
26+
27+
tokens[*ind].expects(LexerTokenType::AngelBracketClose)?;
28+
*ind += 1;
29+
30+
let ty = parse_type(tokens, ind)?;
31+
32+
return Ok(Some(ty));
33+
} else {
34+
return Ok(None);
35+
}
36+
}
37+
2038
pub fn parse_function_declaraction(
2139
tokens: &Vec<LexerToken>,
2240
ind: &mut usize,
@@ -34,12 +52,7 @@ pub fn parse_function_declaraction(
3452

3553
*ind += 1;
3654

37-
let mut ret_type = None;
38-
39-
if tokens[*ind].is_keyword() {
40-
ret_type = Some(parse_type(tokens, ind)?);
41-
//*ind += 1;
42-
}
55+
let ret_type = parse_function_return_type(tokens, ind)?;
4356

4457
tokens[*ind].expects(LexerTokenType::BracketOpen)?;
4558

@@ -108,20 +121,13 @@ pub fn parse_node_body(
108121
let mut tok: &LexerToken = &tokens[*ind];
109122
let mut body: Vec<Box<ASTTreeNode>> = Vec::new();
110123

111-
let mut stock = 1;
112-
113124
while tok.tok_type != LexerTokenType::EndOfFile && tok.tok_type != LexerTokenType::BracketClose
114125
{
115-
if tok.tok_type == LexerTokenType::BracketClose {
116-
stock -= 1;
117-
}
118-
119-
if stock == 0 {
120-
break;
121-
}
126+
if tok.tok_type == LexerTokenType::SemiCollon {
127+
*ind += 1;
128+
tok = &tokens[*ind];
122129

123-
if tok.tok_type == LexerTokenType::BracketOpen {
124-
stock += 1;
130+
continue;
125131
}
126132

127133
let n = parse_ast_node_in_body(tokens, ind)?;

compiler/ast_parser/src/functions/shadow.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use compiler_utils::hash::HashedString;
55
use diagnostics::DiagnosticResult;
66
use lexer::token::{LexerToken, LexerTokenType};
77

8-
use crate::{functions::arguments::parse_function_arguments, types::parse_type};
8+
use crate::functions::{arguments::parse_function_arguments, parse_function_return_type};
99

10-
pub fn parse_shadow_function_declaration(
10+
pub fn parse_extern_function_definition(
1111
tokens: &Vec<LexerToken>,
1212
ind: &mut usize,
1313
) -> DiagnosticResult<Box<ASTTreeNode>> {
@@ -23,19 +23,11 @@ pub fn parse_shadow_function_declaration(
2323

2424
*ind += 1;
2525

26-
let mut ret_type = None;
27-
let end;
28-
29-
if tokens[*ind].is_keyword() {
30-
ret_type = Some(parse_type(tokens, ind)?);
31-
32-
end = tokens[*ind].get_end_pos().clone();
33-
} else {
34-
end = tokens[*ind - 1].get_end_pos().clone();
35-
}
26+
let ret_type = parse_function_return_type(tokens, ind)?;
27+
let end = tokens[*ind].get_end_pos();
3628

3729
return Ok(Box::new(ASTTreeNode::new(
38-
ASTTreeNodeKind::ShadowFunctionDeclaration {
30+
ASTTreeNodeKind::ExternFunctionDeclaration {
3931
func_name: HashedString::new(function_name.0),
4032
args: args.0,
4133
return_type: ret_type,

compiler/ast_parser/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub mod functions;
1515
pub mod literals;
1616
pub mod math;
1717
pub mod parser;
18+
pub mod pointers;
19+
pub mod ranges;
1820
pub mod structs;
1921
pub mod types;
2022
pub mod unwraps;

0 commit comments

Comments
 (0)