1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-09-28 05:17:37 +00:00

feat: file generation

This commit is contained in:
Natapat Samutpong 2022-02-11 18:30:54 +07:00
parent a0f30d19ef
commit 49d70b9a41
4 changed files with 40 additions and 3 deletions

1
def.vyir Normal file
View file

@ -0,0 +1 @@
store 1 number

View file

@ -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 },

View file

@ -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");
}
},
}

View file

@ -1,3 +1,5 @@
use std::fmt;
use crate::front::parser::Value;
#[derive(Debug)]
@ -5,3 +7,12 @@ 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),
}
}
}