Skip to content

Commit

Permalink
Added REPL, Scanner and Compiler
Browse files Browse the repository at this point in the history
Basic scanner works already. Compiler does noting yet
  • Loading branch information
patbuc committed Nov 12, 2023
1 parent 9877be0 commit 58bc2d9
Show file tree
Hide file tree
Showing 6 changed files with 455 additions and 8 deletions.
78 changes: 76 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,92 @@
mod vm;

use colored::Colorize;
use std::io::{Read, Write};
use std::process::exit;

use std::fs::File;
use std::{env, io};

use crate::vm::block::{Block, OpCode};

use crate::vm::vm::VirtualMachine;

fn main() {
print_tagline();

let args: Vec<String> = env::args().collect();

if args.len() == 1 {
run_repl();
} else if args.len() == 2 {
run_file(&args[1]);
} else {
exit(64);
}
}

fn print_tagline() {
println!(
"Hi, this is {} - a toy language you didn't wait for.",
"neon".truecolor(240, 0, 255).bold()
);
}

fn run_repl() {
println!("Running REPL");

let mut vm = VirtualMachine::new();

loop {
print_prompt();

let line = read_line();

vm.interpret(line);

println!();
}
}

fn read_line() -> String {
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
String::from(input.trim())
}

fn print_prompt() {
print!("> ");
io::stdout().flush().unwrap();
}

fn run_file(path: &String) {
println!("Running file: {} ", path);
println!();

let source = read_file(path);
let mut vm = VirtualMachine::new();

let result: vm::vm::Result = vm.interpret(source);
match result {
vm::vm::Result::Ok => return,
vm::vm::Result::CompileError => exit(65),
vm::vm::Result::RuntimeError => exit(70),
}
}

fn read_file(path: &str) -> String {
let mut file = File::open(path).expect(format!("Failed to open the file {}", path).as_str());

// Read the file contents into a string
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect(format!("Failed to read the file {}", path).as_str());
contents
}

fn write_block() -> Block {
let mut block = Block::new("ZeBlock");

block.write_constant(1234.56, 2);
Expand All @@ -22,6 +97,5 @@ fn main() {
block.write_op_code(OpCode::Multiply, 6);
block.write_op_code(OpCode::Return, 8);

let mut vm = VirtualMachine::new(block);
vm.interpret();
block
}
9 changes: 9 additions & 0 deletions src/neon.n
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var x = 123;

if (x == 123 and x > 3) {
print("x is 123");
}

fun test() {

}
12 changes: 12 additions & 0 deletions src/vm/compiler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::vm::scanner::Scanner;

pub(crate) struct Compiler {}

impl Compiler {
pub(super) fn compile(&self, source: String) {
let scanner = Scanner::new(source);
for token in scanner {
println!("{:?}", token);
}
}
}
2 changes: 2 additions & 0 deletions src/vm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod block;
mod compiler;
mod scanner;
pub mod vm;
Loading

0 comments on commit 58bc2d9

Please sign in to comment.