aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2021-09-23 22:52:03 +0200
committerJohannes Stoelp <johannes.stoelp@gmail.com>2021-09-23 22:52:03 +0200
commit425ba77074347f71283f75839224f78bd94f2e10 (patch)
tree7c0eb7fa5e5a95ee662e127c1ca05b9cd391faa5
parentf3a1cd1a5523cdf4cda7925d32674ae969990a30 (diff)
downloadllvm-kaleidoscope-rs-425ba77074347f71283f75839224f78bd94f2e10.tar.gz
llvm-kaleidoscope-rs-425ba77074347f71283f75839224f78bd94f2e10.zip
main: re-structure main repl loop
-rw-r--r--src/main.rs115
1 files changed, 51 insertions, 64 deletions
diff --git a/src/main.rs b/src/main.rs
index be3dede..a5b57d0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,58 +8,62 @@ use llvm_kaleidoscope_rs::{
use std::io::Read;
-fn handle_definition<I>(p: &mut Parser<I>, module: &llvm::Module)
+fn main_loop<I>(mut parser: Parser<I>)
where
I: Iterator<Item = char>,
{
- match p.parse_definition() {
- Ok(func) => {
- println!("Parse 'def'\n{:?}", func);
- if let Ok(func) = Codegen::compile(module, Either::B(&func)) {
- func.dump();
- }
- }
- Err(err) => {
- eprintln!("Error: {:?}", err);
- p.get_next_token();
- }
- }
-}
+ // Initialize LLVM module with its own context.
+ // We will emit LLVM IR into this module.
+ let module = llvm::Module::new();
-fn handle_extern<I>(p: &mut Parser<I>, module: &llvm::Module)
-where
- I: Iterator<Item = char>,
-{
- match p.parse_extern() {
- Ok(proto) => {
- println!("Parse 'extern'\n{:?}", proto);
- if let Ok(proto) = Codegen::compile(module, Either::A(&proto)) {
- proto.dump();
+ loop {
+ match parser.cur_tok() {
+ Token::Eof => break,
+ Token::Char(';') => {
+ // Ignore top-level semicolon.
+ parser.get_next_token();
}
- }
- Err(err) => {
- eprintln!("Error: {:?}", err);
- p.get_next_token();
- }
+ Token::Def => match parser.parse_definition() {
+ Ok(func) => {
+ println!("Parse 'def'\n{:?}", func);
+ if let Ok(func) = Codegen::compile(&module, Either::B(&func)) {
+ func.dump();
+ }
+ }
+ Err(err) => {
+ eprintln!("Error: {:?}", err);
+ parser.get_next_token();
+ }
+ },
+ Token::Extern => match parser.parse_extern() {
+ Ok(proto) => {
+ println!("Parse 'extern'\n{:?}", proto);
+ if let Ok(proto) = Codegen::compile(&module, Either::A(&proto)) {
+ proto.dump();
+ }
+ }
+ Err(err) => {
+ eprintln!("Error: {:?}", err);
+ parser.get_next_token();
+ }
+ },
+ _ => match parser.parse_top_level_expr() {
+ Ok(func) => {
+ println!("Parse top-level expression\n{:?}", func);
+ if let Ok(func) = Codegen::compile(&module, Either::B(&func)) {
+ func.dump();
+ }
+ }
+ Err(err) => {
+ eprintln!("Error: {:?}", err);
+ parser.get_next_token();
+ }
+ },
+ };
}
-}
-fn handle_top_level_expression<I>(p: &mut Parser<I>, module: &llvm::Module)
-where
- I: Iterator<Item = char>,
-{
- match p.parse_top_level_expr() {
- Ok(func) => {
- println!("Parse top-level expression\n{:?}", func);
- if let Ok(func) = Codegen::compile(module, Either::B(&func)) {
- func.dump();
- }
- }
- Err(err) => {
- eprintln!("Error: {:?}", err);
- p.get_next_token();
- }
- }
+ // Dump all the emitted LLVM IR to stdout.
+ module.dump();
}
fn main() {
@@ -79,25 +83,8 @@ fn main() {
// Throw first coin and initialize cur_tok.
parser.get_next_token();
- // Initialize LLVM module with its own context.
- // We will emit LLVM IR into this module.
- let module = llvm::Module::new();
-
- loop {
- match *parser.cur_tok() {
- Token::Eof => break,
- Token::Char(';') => {
- // Ignore top-level semicolon.
- parser.get_next_token()
- }
- Token::Def => handle_definition(&mut parser, &module),
- Token::Extern => handle_extern(&mut parser, &module),
- _ => handle_top_level_expression(&mut parser, &module),
- }
- }
-
- // Dump all the emitted LLVM IR to stdout.
- module.dump();
+ main_loop(parser);
+ // De-allocate managed static LLVM data.
llvm::shutdown();
}