mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
write to file, use console.log
This commit is contained in:
parent
d10f3e3f38
commit
b15f7f1971
|
@ -56,7 +56,7 @@ fn gen_ir(ir: &IR) -> String {
|
||||||
match name.as_str() {
|
match name.as_str() {
|
||||||
"print" => {
|
"print" => {
|
||||||
let args = gen_ir(&args[0]);
|
let args = gen_ir(&args[0]);
|
||||||
format!("process.stdout.write({});", args.trim_end_matches(";"))
|
format!("console.log({});", args.trim_end_matches(";"))
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
let args = args
|
let args = args
|
||||||
|
|
38
src/main.rs
38
src/main.rs
|
@ -1,4 +1,8 @@
|
||||||
use std::{fs, io::Write, time};
|
use std::{
|
||||||
|
fs,
|
||||||
|
io::{self, Write},
|
||||||
|
time,
|
||||||
|
};
|
||||||
|
|
||||||
use chumsky::{Parser, Stream};
|
use chumsky::{Parser, Stream};
|
||||||
use clap::Parser as ArgParser;
|
use clap::Parser as ArgParser;
|
||||||
|
@ -27,21 +31,25 @@ use crate::util::log;
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
match args.options {
|
match args.options {
|
||||||
Options::Compile { input: file_name, ast: _print_ast } => {
|
Options::Compile {
|
||||||
|
input: file_name,
|
||||||
|
ast: _print_ast,
|
||||||
|
} => {
|
||||||
// Get file contents.
|
// Get file contents.
|
||||||
let src = fs::read_to_string(&file_name).expect("Failed to read file");
|
let src = fs::read_to_string(&file_name).expect("Failed to read file");
|
||||||
|
|
||||||
// Lex the file.
|
// Lex the file.
|
||||||
let (tokens, lex_error) = lexer().parse_recovery(src.as_str());
|
let (tokens, lex_error) = lexer().parse_recovery(src.as_str());
|
||||||
let len = src.chars().count();
|
let len = src.chars().count();
|
||||||
|
|
||||||
// Parse the file.
|
// Parse the file.
|
||||||
let (ast, parse_error) = parser().parse_recovery(Stream::from_iter(len..len + 1, tokens.clone().unwrap().into_iter()));
|
let (ast, parse_error) = parser().parse_recovery(Stream::from_iter(
|
||||||
|
len..len + 1,
|
||||||
|
tokens.clone().unwrap().into_iter(),
|
||||||
|
));
|
||||||
|
|
||||||
if lex_error.is_empty() {
|
if lex_error.is_empty() {
|
||||||
|
|
||||||
if parse_error.is_empty() {
|
if parse_error.is_empty() {
|
||||||
|
|
||||||
match ast {
|
match ast {
|
||||||
// If there is some AST then generate code.
|
// If there is some AST then generate code.
|
||||||
Some(ast) => {
|
Some(ast) => {
|
||||||
|
@ -50,22 +58,24 @@ fn main() {
|
||||||
let ir = ir::ast_to_ir(ast);
|
let ir = ir::ast_to_ir(ast);
|
||||||
|
|
||||||
let out = back::js::gen(ir);
|
let out = back::js::gen(ir);
|
||||||
println!("{}", out);
|
|
||||||
|
let file = fs::File::create("out.js").expect("Failed to create file");
|
||||||
|
let mut file = io::BufWriter::new(file);
|
||||||
|
file.write_all(out.as_bytes())
|
||||||
|
.expect("Failed to write file");
|
||||||
|
|
||||||
let all_elapsed = start.elapsed();
|
let all_elapsed = start.elapsed();
|
||||||
log(0, format!("Done in {}s", all_elapsed.as_secs_f64()));
|
log(0, format!("Done in {}s", all_elapsed.as_secs_f64()));
|
||||||
},
|
}
|
||||||
// If there is no AST, then notify the user.
|
// If there is no AST, then notify the user.
|
||||||
None => println!("no ast :("),
|
None => println!("no ast :("),
|
||||||
};
|
};
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
eprintln!("{:#?}\n(Parser error)", parse_error);
|
eprintln!("{:#?}\n(Parser error)", parse_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
eprintln!("{:#?}\n(Lexer error)", lex_error);
|
eprintln!("{:#?}\n(Lexer error)", lex_error);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue