From 05ac6fa2c118be78c21269e06cc719f6d7ab6c09 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sun, 29 Mar 2020 13:27:00 +0800 Subject: [PATCH] read input from file --- src/main.rs | 13 +++++++++---- test.py | 2 ++ 2 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 test.py diff --git a/src/main.rs b/src/main.rs index 60b77626..428c3166 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use std::error::Error; use std::fmt; use std::path::Path; use std::collections::HashMap; +use std::fs; #[derive(Debug)] enum CompileErrorKind { @@ -54,7 +55,7 @@ struct CompileError { impl fmt::Display for CompileError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Compilation error at {}: {}", self.location, self.kind) + write!(f, "{}, at {}", self.kind, self.location) } } @@ -282,16 +283,20 @@ impl<'ctx> CodeGen<'ctx> { fn main() { Target::initialize_all(&InitializationConfig::default()); - let ast = match parser::parse_program("def foo(x: int32, y: int32) -> int32: return x + y") { + let program = match fs::read_to_string("test.py") { + Ok(program) => program, + Err(err) => { println!("Cannot open input file: {}", err); return; } + }; + let ast = match parser::parse_program(&program) { Ok(ast) => ast, - Err(err) => { println!("{}", err); return; } + Err(err) => { println!("Parse error: {}", err); return; } }; let context = Context::create(); let mut codegen = CodeGen::new(&context); match codegen.compile_toplevel(&ast.statements[0]) { Ok(_) => (), - Err(err) => { println!("{}", err); return; } + Err(err) => { println!("Compilation error: {}", err); return; } } codegen.output(); } diff --git a/test.py b/test.py new file mode 100644 index 00000000..57aa3ddf --- /dev/null +++ b/test.py @@ -0,0 +1,2 @@ +def foo(x: int32, y: int32) -> int32: + return x + y