Skip to content

Commit 7c8b4f9

Browse files
author
DigitalCodeCrafter
committed
added some stuff... just look at the diff
1 parent bd59450 commit 7c8b4f9

5 files changed

Lines changed: 179 additions & 30 deletions

File tree

src/assembler/lexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl Lexer {
145145
}
146146
return Some(Ok(Token { kind: TokenType::StringLiteral(s), line: start_line }));
147147
}
148-
'0'..='9' | '.' => {
148+
'0'..='9' | '.' | '-' => {
149149
let mut number = c.to_string();
150150
let mut is_float = c == '.';
151151

src/compiler/ast.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,24 @@ pub enum NodeKind {
1313
Call { callee: NodeId, args: Vec<NodeId> },
1414
Block { nodes: Vec<NodeId> },
1515
If { cond: NodeId, then_block: NodeId, else_block: Option<NodeId> },
16+
Loop { block: NodeId },
1617
Return { expr: Option<NodeId> },
18+
Break { expr: Option<NodeId> }, // labels?
1719
Tuple { elements: Vec<NodeId> },
1820
Array { elements: Vec<NodeId> },
1921
ArrayRepeat { value: NodeId, count: NodeId },
22+
UnderscoreExpr,
23+
IndexExpression { array: NodeId , index: NodeId },
24+
TupleIndexExpression { tuple: NodeId , index: i32 },
2025

2126
// Statements
2227
LetStmt { name: String, mutable: bool, ty: Option<TypeId>, value: Option<NodeId> },
2328
ExprStmt { expr: NodeId },
24-
ItemStmt { item: NodeId },
2529

2630
// Items
27-
Function { name: String, params: Vec<(String, TypeId)>, return_type: Option<TypeId>, body: NodeId },
31+
Function { public: bool, name: String, params: Vec<(String, TypeId)>, return_type: Option<TypeId>, body: NodeId },
32+
Module { public: bool, name: String, items: Vec<NodeId> },
33+
UseDecl { public: bool, },
2834

2935
// Program
3036
Program { items: Vec<NodeId> },

src/compiler/lexer.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@ pub enum TokenKind {
1010
Int(i32),
1111
Float(f64),
1212
String(String),
13-
Bool(bool),
1413

1514
// Keywords
15+
Mod,
16+
Use,
17+
Pub,
18+
True,
19+
False,
1620
Let,
1721
Mut,
1822
Fn,
23+
Return,
1924
If,
2025
Else,
2126
While,
22-
Return,
27+
Loop,
28+
Break,
2329

2430
// Operators
2531
Plus, // +
@@ -52,7 +58,9 @@ pub enum TokenKind {
5258
RBracket, // ]
5359
Comma, // ,
5460
Colon, // :
61+
ColCol, // ::
5562
Semi, // ;
63+
Underscore, // _
5664

5765
// Misc
5866
EOF,
@@ -107,6 +115,10 @@ impl Lexer {
107115
self.lex_identifier_or_keyword(start_line, start_col)
108116
} else if c.is_ascii_digit() {
109117
self.lex_number(start_line, start_col)
118+
} else if c == '.' || self.peek_ahead(1).map_or(false, |n| n.is_ascii_digit()) {
119+
self.lex_number(start_line, start_col)
120+
} else if c == '-' || self.peek_ahead(1).map_or(false, |n| n.is_ascii_digit()) {
121+
self.lex_number(start_line, start_col)
110122
} else if c == '"' {
111123
self.lex_string(start_line, start_col)?
112124
} else {
@@ -191,15 +203,20 @@ impl Lexer {
191203
}
192204

193205
let kind = match s.as_str() {
206+
"mod" => TokenKind::Mod,
207+
"use" => TokenKind::Use,
208+
"pub" => TokenKind::Pub,
209+
"true" => TokenKind::True,
210+
"false" => TokenKind::False,
194211
"let" => TokenKind::Let,
195212
"mut" => TokenKind::Mut,
196213
"fn" => TokenKind::Fn,
214+
"return" => TokenKind::Return,
197215
"if" => TokenKind::If,
198216
"else" => TokenKind::Else,
199217
"while" => TokenKind::While,
200-
"return" => TokenKind::Return,
201-
"true" => TokenKind::Bool(true),
202-
"false" => TokenKind::Bool(false),
218+
"loop" => TokenKind::Loop,
219+
"break" => TokenKind::Break,
203220
_ => TokenKind::Identifier(s),
204221
};
205222

@@ -234,7 +251,7 @@ impl Lexer {
234251
}
235252
}
236253

237-
// decimal or floating
254+
// decimal or floating
238255
while let Some(c) = self.peek() {
239256
if c.is_ascii_digit() || c == '_' {
240257
num_str.push(c);
@@ -243,6 +260,9 @@ impl Lexer {
243260
has_dot = true;
244261
num_str.push(c);
245262
self.advance();
263+
} else if c == '-' && num_str.len() == 0 && self.peek_ahead(1).map_or(false, |n| n.is_ascii_digit()) {
264+
num_str.push(c);
265+
self.advance();
246266
} else if (c == 'e' || c == 'E') && !has_exp {
247267
if let Some(next) = self.peek_ahead(1) {
248268
if next.is_ascii_digit() {
@@ -366,8 +386,10 @@ impl Lexer {
366386
']' => RBracket,
367387
',' => Comma,
368388
';' => Semi,
389+
':' if self.match_next(':') => ColCol,
369390
':' => Colon,
370391
'.' => Dot,
392+
'_' => Underscore,
371393
_ => return Err(CompilerError::LexerUnexpectedChar { line, col, c }),
372394
};
373395

src/compiler/parser.rs

Lines changed: 120 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,23 @@ impl Parser {
158158

159159
impl Parser {
160160
fn parse_item(&mut self) -> PResult<NodeId> {
161+
let public = match self.peek() {
162+
TokenKind::Pub => {
163+
self.next();
164+
true
165+
},
166+
_ => false
167+
};
168+
161169
match self.peek_with_span() {
162-
(TokenKind::Fn, _) => self.parse_function(),
170+
(TokenKind::Mod, _) => self.parse_module(public),
171+
(TokenKind::Use, _) => self.parse_use(public),
172+
(TokenKind::Fn, _) => self.parse_function(public),
163173
(other, span) => Err(ParseError::expected(span, vec![TokenKind::Fn], other.clone())),
164174
}
165175
}
166176

167-
fn parse_function(&mut self) -> PResult<NodeId> {
177+
fn parse_function(&mut self, public: bool) -> PResult<NodeId> {
168178
self.start_span();
169179
self.expect(TokenKind::Fn)?;
170180

@@ -182,7 +192,7 @@ impl Parser {
182192

183193
let body = self.parse_block_expression()?;
184194

185-
self.push_node(NodeKind::Function { name, params, return_type, body })
195+
self.push_node(NodeKind::Function { public, name, params, return_type, body })
186196
}
187197

188198
fn parse_parameters(&mut self) -> PResult<Vec<(String, TypeId)>> {
@@ -213,6 +223,44 @@ impl Parser {
213223
self.expect(TokenKind::RParen)?;
214224
Ok(params)
215225
}
226+
227+
fn parse_module(&mut self, public: bool) -> PResult<NodeId> {
228+
self.start_span();
229+
self.expect(TokenKind::Mod)?;
230+
231+
let name = match self.next_with_span() {
232+
(TokenKind::Identifier(name), _) => name.clone(),
233+
(other, span) => return Err(ParseError::new(span, format!("expected module name, found {:?}", other))),
234+
};
235+
236+
let mut items = Vec::new();
237+
238+
match self.next_with_span() {
239+
(TokenKind::Semi, _) => {},
240+
(TokenKind::LBrace, _) => {
241+
while !matches!(self.peek(), TokenKind::EOF) {
242+
items.push(self.parse_item()?)
243+
}
244+
self.expect(TokenKind::RBrace)?;
245+
}
246+
247+
(other, span) => return Err(ParseError::expected(span, vec![TokenKind::Semi, TokenKind::LBrace], other.clone())),
248+
}
249+
250+
self.push_node(NodeKind::Module { public, name, items })
251+
}
252+
253+
fn parse_use(&mut self, public: bool) -> PResult<NodeId> {
254+
self.start_span();
255+
self.expect(TokenKind::Use)?;
256+
257+
// Use tree
258+
todo!();
259+
260+
self.expect(TokenKind::Semi)?;
261+
262+
self.push_node(NodeKind::UseDecl { public })
263+
}
216264
}
217265

218266
// --- Statements ---
@@ -221,11 +269,7 @@ impl Parser {
221269
fn parse_statement(&mut self) -> PResult<NodeId> {
222270
match self.peek() {
223271
TokenKind::Let => self.parse_let_statement(),
224-
TokenKind::Fn => {
225-
self.start_span();
226-
let item = self.parse_function()?;
227-
self.push_node(NodeKind::ItemStmt { item })
228-
}
272+
TokenKind::Fn | TokenKind::Use | TokenKind::Mod | TokenKind::Pub => self.parse_item(),
229273
_ => {
230274
self.start_span();
231275
let expr = self.parse_expression(0)?;
@@ -284,7 +328,7 @@ impl Parser {
284328
loop {
285329
let op = self.peek().clone();
286330

287-
if let Some(expr) = self.try_parse_postfix(&op, lhs)? {
331+
if let Some(expr) = self.try_parse_postfix(&op, lhs, start)? {
288332
lhs = expr;
289333
continue;
290334
}
@@ -327,8 +371,14 @@ impl Parser {
327371
self.next();
328372
self.push_node(kind)
329373
}
330-
(TokenKind::Bool(b), _) => {
331-
let kind = NodeKind::Literal(Literal::Bool(*b));
374+
(TokenKind::True, _) => {
375+
let kind = NodeKind::Literal(Literal::Bool(true));
376+
self.start_span();
377+
self.next();
378+
self.push_node(kind)
379+
}
380+
(TokenKind::False, _) => {
381+
let kind = NodeKind::Literal(Literal::Bool(false));
332382
self.start_span();
333383
self.next();
334384
self.push_node(kind)
@@ -339,6 +389,11 @@ impl Parser {
339389
self.next();
340390
self.push_node(kind)
341391
}
392+
(TokenKind::Underscore, _) => {
393+
self.start_span();
394+
self.next();
395+
self.push_node(NodeKind::UnderscoreExpr)
396+
}
342397

343398
// Unary ops
344399
(TokenKind::Minus | TokenKind::Not, _) => {
@@ -390,9 +445,24 @@ impl Parser {
390445
// if expression
391446
(TokenKind::If, _) => self.parse_if_expression(),
392447

448+
// Loop expression
449+
(TokenKind::Loop, _) => self.parse_loop_expression(),
450+
393451
// Block expression
394452
(TokenKind::LBrace, _) => self.parse_block_expression(),
395453

454+
// break expression
455+
(TokenKind::Break, _) => {
456+
self.start_span();
457+
self.next();
458+
if matches!(self.peek(), TokenKind::Semi | TokenKind::RBrace | TokenKind::EOF) {
459+
self.push_node(NodeKind::Break { expr: None })
460+
} else {
461+
let expr = self.parse_expression(0)?;
462+
self.push_node(NodeKind::Break { expr: Some(expr) })
463+
}
464+
}
465+
396466
// return expression
397467
(TokenKind::Return, _) => {
398468
self.start_span();
@@ -409,11 +479,11 @@ impl Parser {
409479
}
410480
}
411481

412-
fn try_parse_postfix(&mut self, op: &TokenKind, lhs: NodeId) -> PResult<Option<NodeId>> {
482+
fn try_parse_postfix(&mut self, op: &TokenKind, lhs: NodeId, start: Pos) -> PResult<Option<NodeId>> {
413483
// Function call
414484
if matches!(op, TokenKind::LParen) {
415485
self.next();
416-
486+
417487
let mut args = Vec::new();
418488
if !matches!(self.peek(), TokenKind::RParen) {
419489
loop {
@@ -430,9 +500,35 @@ impl Parser {
430500
}
431501
}
432502
self.expect(TokenKind::RParen)?;
433-
503+
504+
self.pos_stack.push(start);
434505
return Ok(Some(self.push_node(NodeKind::Call { callee: lhs, args })?));
435506
}
507+
// array indexing
508+
if matches!(op, TokenKind::LBracket) {
509+
self.next();
510+
511+
let index = self.parse_expression(0)?;
512+
513+
self.expect(TokenKind::RBracket)?;
514+
515+
self.pos_stack.push(start);
516+
return Ok(Some(self.push_node(NodeKind::IndexExpression { array: lhs, index })?));
517+
}
518+
// tuple indexing / field access
519+
if matches!(op, TokenKind::Dot) {
520+
self.next();
521+
522+
match self.next_with_span() {
523+
(TokenKind::Identifier(_field), _) => todo!("Field Access is not yet implemented"),
524+
(TokenKind::Int(i), _) => {
525+
let index = *i;
526+
self.pos_stack.push(start);
527+
return Ok(Some(self.push_node(NodeKind::TupleIndexExpression { tuple: lhs, index } )?));
528+
}
529+
(other, span) => return Err(ParseError::new(span, format!("expected field name or tuple index, found {:?}", other))),
530+
}
531+
}
436532

437533
Ok(None)
438534
}
@@ -456,6 +552,15 @@ impl Parser {
456552
self.push_node(NodeKind::If { cond, then_block, else_block })
457553
}
458554

555+
fn parse_loop_expression(&mut self) -> PResult<NodeId> {
556+
self.start_span();
557+
self.expect(TokenKind::Loop)?;
558+
559+
let block = self.parse_block_expression()?;
560+
561+
self.push_node(NodeKind::Loop { block })
562+
}
563+
459564
fn parse_block_expression(&mut self) -> PResult<NodeId> {
460565
self.start_span();
461566
self.expect(TokenKind::LBrace)?;
@@ -774,7 +879,7 @@ mod tests {
774879
let src = r#"
775880
fn main() {
776881
fn local(x: Int) -> Int { x * 2 }
777-
let x = 5;
882+
let mut x: [(List<Int>, Int); 3] = [(List(_), 923); 3];
778883
x + 5 = x - 1;
779884
}
780885
"#;

0 commit comments

Comments
 (0)