mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
feat: file generation
This commit is contained in:
parent
a0f30d19ef
commit
49d70b9a41
|
@ -17,6 +17,26 @@ pub enum Value {
|
|||
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)]
|
||||
pub enum Tree {
|
||||
Atom { atom: Value, quote: bool },
|
||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -1,4 +1,4 @@
|
|||
use std::fs::read_to_string;
|
||||
use std::{fs::{ read_to_string, File }, io::Write};
|
||||
use clap::Parser;
|
||||
|
||||
/// Arguments handler.
|
||||
|
@ -20,11 +20,12 @@ fn main() {
|
|||
let args = Args::parse();
|
||||
match args.options {
|
||||
Options::Compile { input, ast } => {
|
||||
let code = read_to_string(input).unwrap();
|
||||
let code = read_to_string(&input).unwrap();
|
||||
let tree = parse(&code);
|
||||
match ast {
|
||||
true => for node in tree { println!("{:#?}", node) },
|
||||
false => {
|
||||
// Check if the tree is valid
|
||||
let mut checked_tree = Vec::new();
|
||||
for node in tree {
|
||||
match node {
|
||||
|
@ -33,9 +34,13 @@ fn main() {
|
|||
}
|
||||
};
|
||||
|
||||
// Generate instructions
|
||||
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 {
|
||||
println!("{:#?}", instruction);
|
||||
file.write_all(instruction.to_string().as_bytes()).expect("Failed to write instructions to file");
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
|
@ -1,7 +1,18 @@
|
|||
use std::fmt;
|
||||
|
||||
use crate::front::parser::Value;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Instructions {
|
||||
Store { value: Value, name: Box<str> },
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue