From 49d70b9a4176397070f8271210d6829ed6244e55 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Fri, 11 Feb 2022 18:30:54 +0700 Subject: [PATCH] feat: file generation --- def.vyir | 1 + src/front/parser.rs | 20 ++++++++++++++++++++ src/main.rs | 11 ++++++++--- src/middle/instr.rs | 11 +++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 def.vyir diff --git a/def.vyir b/def.vyir new file mode 100644 index 0000000..5b7a5f8 --- /dev/null +++ b/def.vyir @@ -0,0 +1 @@ +store 1 number \ No newline at end of file diff --git a/src/front/parser.rs b/src/front/parser.rs index 2036c5a..5960f92 100644 --- a/src/front/parser.rs +++ b/src/front/parser.rs @@ -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 }, diff --git a/src/main.rs b/src/main.rs index 822541a..f468160 100644 --- a/src/main.rs +++ b/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"); } }, } diff --git a/src/middle/instr.rs b/src/middle/instr.rs index 6435285..2911922 100644 --- a/src/middle/instr.rs +++ b/src/middle/instr.rs @@ -1,7 +1,18 @@ +use std::fmt; + use crate::front::parser::Value; #[derive(Debug)] pub enum Instructions { Store { value: Value, name: Box }, 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), + } + } } \ No newline at end of file