Skip to content

Commit

Permalink
print statement
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilipsiva committed Apr 12, 2023
1 parent 355435d commit ef4cbef
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ edition = "2021"
log = "0.4.17"
pest = "2.5.7"
pest_derive = "2.5.7"
thiserror = "1.0.40"
2 changes: 1 addition & 1 deletion examples/01-basics.oduraja
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Log |5|
Log |5 + 5|
4 changes: 2 additions & 2 deletions src/main.pest
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ stmt_invoke = { !reserved_parts ~ part ~ (param_invoke | part)* }
stmt_break = { BREAK }
stmt_continue = { CONTINUE }
stmt_return = { RETURN ~ (param_invoke)? }
param_invoke = _{ "|" ~ expression ~ "|" }
param_define = _{ "|" ~ ident ~ "|" }
param_invoke = { "|" ~ expression ~ "|" }
param_define = { "|" ~ ident ~ "|" }

expression = _{ infix | expression_inner }
infix = { expression_inner ~ (binary_operator ~ expression_inner)+ }
Expand Down
120 changes: 108 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,105 @@ use pest::{
// pratt_parser::{Assoc::*, Op, PrattParser},
Parser,
};
use std::fs::read_to_string;
use std::{collections::HashMap, fs::read_to_string};

fn oduraja(pair: Pair<Rule>) {
use thiserror::Error;

#[derive(Error, Debug)]
pub enum OduRajaError {
#[error("Variable not defined")]
VariableNotDefined(String),
#[error("Statement not defined")]
StatementNotDefined(String),
}

#[derive(Clone, Debug)]
enum Literal {
Int(i32),
Float(f32),
Bool(bool),
String(String),
List(Vec<Literal>),
Dict(HashMap<String, Literal>),
}

#[derive(Clone, Default)]
struct Context {
variables: HashMap<String, Literal>,
statements: HashMap<String, Callback>,
}

impl Context {
fn get_variable(&self, name: &String) -> Option<&Literal> {
self.variables.get(name)
}

fn contains_variable(&self, name: &String) -> bool {
self.variables.contains_key(name)
}

fn contains_statement(&self, name: &String) -> bool {
self.statements.contains_key(name)
}

fn get_statement(&self, name: &String) -> Option<&Callback> {
self.statements.get(name)
}

fn init_statement(&mut self) {
self.statements.insert("log|param|".into(), log_param);
}
}

type Callback = fn(&Pair<Rule>, &mut Context) -> Result<(), OduRajaError>;

fn hashify(text: &str) -> String {
text.replace(' ', "").to_lowercase()
}

fn get_stmt_hash(pair: &Pair<Rule>) -> String {
let mut hash = String::new();
for pair in pair.clone().into_inner() {
match pair.as_rule() {
Rule::part => hash.push_str(&hashify(pair.as_str())),
Rule::param_invoke | Rule::param_define => hash.push_str("|param|"),
_ => unreachable!(),
}
}
hash
}

fn log_param(pair: &Pair<Rule>, globals: &mut Context) -> Result<(), OduRajaError> {
let value = pair
.clone()
.into_inner()
.nth(1)
.unwrap()
.into_inner()
.last();
dbg!(value);
// let value = oduraja(&value, globals);
Ok(())
}

fn stmt_invoke(pair: &Pair<Rule>, globals: &mut Context) -> Result<(), OduRajaError> {
let hash = get_stmt_hash(pair);
if !globals.contains_statement(&hash) {
return Err(OduRajaError::StatementNotDefined(pair.as_str().into()));
}
let statement = globals.get_statement(&hash).unwrap();
statement(pair, globals)
}

fn oduraja(pair: &Pair<Rule>, globals: &mut Context) -> Result<(), OduRajaError> {
match pair.as_rule() {
Rule::EOI => todo!(),
Rule::WHITESPACE => todo!(),
Rule::oduraja => todo!(),
Rule::reserved => todo!(),
Rule::part => todo!(),
Rule::statements => todo!(),
Rule::stmt_invoke => todo!(),
Rule::stmt_invoke => stmt_invoke(pair, globals),
Rule::stmt_define => todo!(),
Rule::stmt_assign => todo!(),
Rule::param_invoke => todo!(),
Expand Down Expand Up @@ -89,22 +177,30 @@ fn oduraja(pair: Pair<Rule>) {
Rule::stmt_continue => todo!(),
Rule::stmt_return => todo!(),
Rule::primary => todo!(),
};
/*
let _pratt = PrattParser::new()
.op(Op::infix(Rule::add, Left) | Op::infix(Rule::sub, Left))
.op(Op::infix(Rule::mul, Left) | Op::infix(Rule::div, Left))
.op(Op::prefix(Rule::neg));
*/
}
}

fn main() {
let source = read_to_string("examples/01-basics.oduraja").unwrap();

/*
let pratt = PrattParser::new()
.op(Op::infix(Rule::plus, Left) | Op::infix(Rule::minus, Left))
.op(Op::infix(Rule::multiply, Left) | Op::infix(Rule::divide, Left))
.op(Op::prefix(Rule::unary));
*/
match parser::Parser::parse(Rule::oduraja, &source) {
Ok(tree) => {
dbg!(&tree);
let mut context = Context::default();
context.init_statement();
for pair in tree {
oduraja(pair);
match oduraja(&pair, &mut context) {
Ok(_) => todo!(),
Err(err) => {
dbg!(err);
panic!()
}
};
}
}
Err(err) => {
Expand Down

0 comments on commit ef4cbef

Please sign in to comment.