From 7831cb8d8a9134e47d69517b9b8f610f512561ed Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 8 Sep 2017 16:42:42 -0700 Subject: [PATCH] Start parsing --- src/schala_lang/mod.rs | 14 +++++++------- src/schala_lang/parsing.rs | 39 ++++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/schala_lang/mod.rs b/src/schala_lang/mod.rs index b289055..349c72a 100644 --- a/src/schala_lang/mod.rs +++ b/src/schala_lang/mod.rs @@ -23,13 +23,14 @@ impl ProgrammingLanguageInterface for Schala { output.add_artifact(TraceArtifact::new("tokens", format!("{:?}", tokens))); } - let token_errors: Vec<&String> = tokens.iter().filter_map(|t| t.get_error()).collect(); - if token_errors.len() != 0 { - output.add_output(format!("Tokenization error: {:?}\n", token_errors)); - return output; + { + let token_errors: Vec<&String> = tokens.iter().filter_map(|t| t.get_error()).collect(); + if token_errors.len() != 0 { + output.add_output(format!("Tokenization error: {:?}\n", token_errors)); + return output; + } } - /* let ast = match parsing::parse(tokens) { Ok(ast) => { if options.debug_parse { @@ -42,9 +43,8 @@ impl ProgrammingLanguageInterface for Schala { return output; } }; - */ - let evaluation_output = format!("{:?}", tokens); + let evaluation_output = format!("{:?}", ast); output.add_output(evaluation_output); return output; } diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index 8c75f74..bf773f6 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -7,7 +7,6 @@ use std::iter::{Enumerate, Peekable}; use self::itertools::Itertools; use std::str::Chars; -#[allow(dead_code)] #[derive(Debug, PartialEq)] pub enum TokenType { Newline, Semicolon, @@ -276,11 +275,35 @@ postop := ε | LParen exprlist RParen | LBracket expression RBracket op := '+', '-', etc. */ -#[allow(dead_code)] -#[derive(Debug)] -pub struct AST { } - -#[allow(dead_code)] -pub fn parse(_input: Vec) -> Result { - Ok(AST { }) +struct Parser { + tokens: Vec, +} + +#[derive(Debug)] +pub struct AST(Vec); + +#[derive(Debug, PartialEq)] +pub enum Statement { + Expression(Expression), + Declaration(Declaration), +} + +#[derive(Debug, PartialEq)] +pub enum Declaration { + FuncDecl, + TypeDecl +} + +#[derive(Debug, PartialEq)] +pub enum Expression { + UnsignedIntLiteral(u64), + SignedIntLiteral(i64), + FloatLiteral(f64), +} + +pub fn parse(input: Vec) -> Result { + use self::Statement::*; use self::Declaration::*; use self::Expression::*; + + let statements = vec![Expression(UnsignedIntLiteral(1))]; + Ok(AST(statements)) }