1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

Compare commits

..

2 commits

Author SHA1 Message Date
Natapat Samutpong cb0efd3dd4 refactor: remove unused 2022-02-11 18:31:08 +07:00
Natapat Samutpong 49d70b9a41 feat: file generation 2022-02-11 18:30:54 +07:00
3 changed files with 39 additions and 3 deletions

View file

@ -17,6 +17,26 @@ pub enum Value {
Nil, Nil,
} }
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Value::True => write!(f, "#t"),
Value::False => write!(f, "#f"),
Value::Int(i) => write!(f, "{}", i),
Value::Float(fl) => write!(f, "{}", fl),
Value::String(s) => write!(f, "{}", s),
Value::Symbol(s) => write!(f, "{}", s),
Value::List(car, cdr) => {
write!(f, "(")?;
write!(f, "{}", car)?;
for item in cdr.iter().cloned() { write!(f, " {}", item)?; }
write!(f, ")")
},
Value::Nil => write!(f, "nil"),
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Tree { pub enum Tree {
Atom { atom: Value, quote: bool }, Atom { atom: Value, quote: bool },

View file

@ -1,4 +1,4 @@
use std::fs::read_to_string; use std::{fs::{ read_to_string, File }, io::Write};
use clap::Parser; use clap::Parser;
/// Arguments handler. /// Arguments handler.
@ -20,11 +20,12 @@ fn main() {
let args = Args::parse(); let args = Args::parse();
match args.options { match args.options {
Options::Compile { input, ast } => { Options::Compile { input, ast } => {
let code = read_to_string(input).unwrap(); let code = read_to_string(&input).unwrap();
let tree = parse(&code); let tree = parse(&code);
match ast { match ast {
true => for node in tree { println!("{:#?}", node) }, true => for node in tree { println!("{:#?}", node) },
false => { false => {
// Check if the tree is valid
let mut checked_tree = Vec::new(); let mut checked_tree = Vec::new();
for node in tree { for node in tree {
match node { match node {
@ -33,9 +34,13 @@ fn main() {
} }
}; };
// Generate instructions
let instructions = generate_instructions(checked_tree.into_iter()); let instructions = generate_instructions(checked_tree.into_iter());
// Write instructions to file
let mut file = File::create(format!("{}.vyir" , input.file_stem().unwrap().to_str().unwrap())).unwrap();
for instruction in instructions { for instruction in instructions {
println!("{:#?}", instruction); file.write_all(instruction.to_string().as_bytes()).expect("Failed to write instructions to file");
} }
}, },
} }

View file

@ -1,3 +1,5 @@
use std::fmt;
use crate::front::parser::Value; use crate::front::parser::Value;
#[derive(Debug)] #[derive(Debug)]
@ -5,3 +7,12 @@ pub enum Instructions {
Store { value: Value, name: Box<str> }, Store { value: Value, name: Box<str> },
Push { value: Value }, Push { value: Value },
} }
impl fmt::Display for Instructions {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Instructions::Store { value, name } => write!(f, "store {} {}", value, name),
Instructions::Push { value } => write!(f, "push {}", value),
}
}
}