Browse Source

Add curly braces

github-actions
Garrit Franke 2 years ago
parent
commit
01981882c2
  1. 4
      examples/hello_world.fx
  2. 11
      examples/math.fx
  3. 6
      src/lexer/mod.rs
  4. 19
      src/main.rs

4
examples/hello_world.fx

@ -0,0 +1,4 @@
fn main() {
let message = "Hello World!"
print(message)
}

11
examples/math.fx

@ -0,0 +1,11 @@
fn main() {
print(fib(10))
}
fn fib(n: int): int {
if (n <= 1) {
return n
}
return fib(n-1) + fib(n-2)
}

6
src/lexer/mod.rs

@ -59,6 +59,10 @@ pub enum TokenKind {
SquareBraceOpen,
/// "]"
SquareBraceClose,
/// "{"
CurlyBracesOpen,
/// "}"
CurlyBracesClose,
/// "\t"
Tab,
/// "\n"
@ -159,6 +163,8 @@ impl Cursor<'_> {
')' => BraceClose,
'[' => SquareBraceOpen,
']' => SquareBraceClose,
'{' => CurlyBracesOpen,
'}' => CurlyBracesClose,
c if is_id_start(c) => {
let kind = self.identifier(c);

19
src/main.rs

@ -1,9 +1,22 @@
use lexer::TokenKind;
use std::fs::File;
use std::io::Read;
mod lexer;
mod parser;
fn main() {
let tokens = lexer::tokenize(&"let x = 2");
fn main() -> std::io::Result<()> {
let mut file = File::open("examples/hello_world.fx")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let tokens = lexer::tokenize(&contents);
// let ast = parser::parse(tokens.into_iter());
println!("{:?}", tokens)
for token in tokens {
if token.kind != TokenKind::Whitespace && token.kind != TokenKind::CarriageReturn {
println!("{:?}", token);
}
}
Ok(())
}

Loading…
Cancel
Save