From 5b24bd80aca7f9476b3a817f1b2877dabae5169d Mon Sep 17 00:00:00 2001 From: teridax Date: Wed, 3 May 2023 12:46:27 +0200 Subject: [PATCH] fixed minor issues --- src/builtin/modules/conv.rs | 6 ++-- src/builtin/modules/io.rs | 10 +++++++ src/main.rs | 11 ++++++-- src/parser/data.rs | 2 +- src/token/mod.rs | 6 ++-- src/vmrt/mod.rs | 14 +++++++++- test.erpn | 56 ++++++++++++++++--------------------- test.yard | 31 ++++++-------------- 8 files changed, 73 insertions(+), 63 deletions(-) diff --git a/src/builtin/modules/conv.rs b/src/builtin/modules/conv.rs index d511431..eabe404 100644 --- a/src/builtin/modules/conv.rs +++ b/src/builtin/modules/conv.rs @@ -16,14 +16,16 @@ pub fn rat_to_int(args: &[Data]) -> Result, ()> { pub fn str_to_int(args: &[Data]) -> Result, ()> { match &args[0] { - Data::Str(val) => Ok(Some(Data::Int(val.parse().unwrap_or(0)))), + Data::Str(val) => { + Ok(Some(Data::Int(val.trim().parse().unwrap_or(0)))) + }, _ => Err(()) } } pub fn str_to_rat(args: &[Data]) -> Result, ()> { match &args[0] { - Data::Str(val) => Ok(Some(Data::Rat(val.parse().unwrap_or(0.0)))), + Data::Str(val) => Ok(Some(Data::Rat(val.trim().parse().unwrap_or(0.0)))), _ => Err(()) } } diff --git a/src/builtin/modules/io.rs b/src/builtin/modules/io.rs index 58922bf..240032b 100644 --- a/src/builtin/modules/io.rs +++ b/src/builtin/modules/io.rs @@ -12,6 +12,12 @@ pub fn println(args: &[Data]) -> Result, ()> { Ok(None) } +pub fn read_line(_: &[Data]) -> Result, ()> { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).unwrap(); + Ok(Some(Data::Str(buf))) +} + pub fn get_module_funs<'a>() -> Vec { vec![ BuiltinFun { @@ -22,5 +28,9 @@ pub fn get_module_funs<'a>() -> Vec { declr: Declr::generate_builtin("println", vec![("text", Prim::Str)], None), func: &println }, + BuiltinFun { + declr: Declr::generate_builtin("readline", vec![], Some(Prim::Str)), + func: &read_line + }, ] } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1f73678..298b3fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,14 +40,21 @@ fn compile(settings: &Settings) -> Option<(Vec, Vec, Vec { impl<'a> Declr<'a> { - pub fn generate_builtin(name: &'static str, args: Vec<(&'static str, Prim)>, ret: Option) -> Declr { + pub fn generate_builtin(name: &'static str, args: Vec<(&'static str, Prim)>, ret: Option) -> Declr<'a> { Declr { name: Some(name), args: if args.is_empty() { diff --git a/src/token/mod.rs b/src/token/mod.rs index f759f49..f8b0855 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -150,7 +150,7 @@ impl Operator { } for (x, typ) in types.iter().enumerate() { - if *typ != operands[x] { + if *typ != operands[operands.len() - x - 1] { return Err(()); } } @@ -596,8 +596,8 @@ const TOKEN_REGEX_SRC: &'static str = concat!( r"|'([a-zA-Z0-9_]+)", // labels: 'example r"|(goto\s+[a-zA-Z0-9_]+", // goto example r"|despite|until|loop|break|cont|ret|yield|please)", // keywords - r"|[\W\s]{0,1}(int|rat|bool|str)[\W\s]{0,1}", // raw data types - r"|(true|false|ye|no|maybe)", // boolean values + r"|\b(int|rat|bool|str)\b", // raw data types + r"|\b(true|false|ye|no|maybe)\b", // boolean values r"|([A-Za-z_]+)\s*(?::\s*([a-zA-Z0-9_]+))?\s*=[^=]", // assignment var:int= r"|([A-Za-z_]+)\s*(?::\s*([a-zA-Z0-9_]+))", // declaration var:int r"|([A-Za-z_]+)", // symbol diff --git a/src/vmrt/mod.rs b/src/vmrt/mod.rs index 9995443..5483ace 100644 --- a/src/vmrt/mod.rs +++ b/src/vmrt/mod.rs @@ -323,32 +323,44 @@ fn parse_term<'a>( Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpEq)), Prim::Rat => Instr::Operation(Operation::Rat(RatOp::CmpEq)), Prim::Bool => Instr::Operation(Operation::Bool(BoolOp::CmpEq)), + Prim::Num(NumHint::Int) => Instr::Operation(Operation::Int(IntOp::CmpEq)), + Prim::Num(NumHint::Rat) => Instr::Operation(Operation::Rat(RatOp::CmpEq)), _ => panic!(), }, crate::token::Operator::NotEq => match hint.unwrap() { Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpNEq)), Prim::Rat => Instr::Operation(Operation::Rat(RatOp::CmpNEq)), Prim::Bool => Instr::Operation(Operation::Bool(BoolOp::CmpNEq)), + Prim::Num(NumHint::Int) => Instr::Operation(Operation::Int(IntOp::CmpNEq)), + Prim::Num(NumHint::Rat) => Instr::Operation(Operation::Rat(RatOp::CmpNEq)), _ => panic!(), }, crate::token::Operator::Lt => match hint.unwrap() { Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpLt)), Prim::Rat => Instr::Operation(Operation::Rat(RatOp::CmpLt)), + Prim::Num(NumHint::Int) => Instr::Operation(Operation::Int(IntOp::CmpLt)), + Prim::Num(NumHint::Rat) => Instr::Operation(Operation::Rat(RatOp::CmpLt)), _ => panic!(), }, crate::token::Operator::Gt => match hint.unwrap() { Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpGt)), Prim::Rat => Instr::Operation(Operation::Rat(RatOp::CmpGt)), + Prim::Num(NumHint::Int) => Instr::Operation(Operation::Int(IntOp::CmpGt)), + Prim::Num(NumHint::Rat) => Instr::Operation(Operation::Rat(RatOp::CmpGt)), _ => panic!(), }, crate::token::Operator::GtEq => match hint.unwrap() { Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpGtEq)), Prim::Rat => Instr::Operation(Operation::Rat(RatOp::CmpGtEq)), + Prim::Num(NumHint::Int) => Instr::Operation(Operation::Int(IntOp::CmpGtEq)), + Prim::Num(NumHint::Rat) => Instr::Operation(Operation::Rat(RatOp::CmpGtEq)), _ => panic!(), }, crate::token::Operator::LtEq => match hint.unwrap() { Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpLtEq)), Prim::Rat => Instr::Operation(Operation::Rat(RatOp::CmpLtEq)), + Prim::Num(NumHint::Int) => Instr::Operation(Operation::Int(IntOp::CmpLtEq)), + Prim::Num(NumHint::Rat) => Instr::Operation(Operation::Rat(RatOp::CmpLtEq)), _ => panic!(), }, @@ -742,7 +754,7 @@ pub fn execute(prog: &Program) -> Result { return Ok(exit_code.to_int()?); } - crate::message(crate::token::MessageType::Critical, "main procedure did not return exit code"); + crate::message(crate::token::MessageType::Critical, "main function did not return exit code"); return Err(()); } else { crate::message( diff --git a/test.erpn b/test.erpn index c5f1886..28e5ad9 100644 --- a/test.erpn +++ b/test.erpn @@ -4,6 +4,8 @@ extern function print extern function println +extern function readline + extern function to_rat extern function to_int @@ -12,10 +14,22 @@ extern function parse_int extern function parse_rat +extern function cos + +extern function pow + +extern function sin + +extern function tan + +extern function ln + +extern function sqrt + fac: Load Arg x - Load Int 1 - NotEq Int + Load Int 2 + Gt Num(Int) Unless Load Int 1 Yield @@ -24,40 +38,18 @@ fac: Sub Int Call fac Load Arg x - Mul Int - Yield - -number: - Load Rat 12.0 + Load Int 2 + Sub Int + Call fac + Add Int main: - Call number - Call to_int + Load Int 24 Call fac - Store Int result - Load String "The Factorial of " - Call number - Load String " is: " - Load Var result - Cat Str - Cat Str + Store Int c + Load String "factorial: " + Load Var c Cat Str Call println Load Int 0 - Store Int x - Loopstart - Load Var x - Load Int 9 - Gt Int - While - Load String "" - Load Var x - Cat Str - Call println - Load Var x - Load Int 1 - Add Int - Store Int x - Load Int 0 - Yield diff --git a/test.yard b/test.yard index c1f455c..c46be2d 100644 --- a/test.yard +++ b/test.yard @@ -1,31 +1,18 @@ -@feature(io, conv) +@feature(io, conv, math) @version(100) @author(Sven Vogel) --- compute the factorial --- in a recursive and functional way -fac(x:int) = int { - despite x != 1 { - yield 1; +max(a:int, b:int)=int { + despite a < b { + yield a } - - yield fac(x - 1) * x + b } -number = rat 12.0 # this is a function without arguments and a braceless body - -// main function +# main function main = int { - result = fac(to_int(number)); + println("" .. max(3,4)); - println("The Factorial of " .. number() .. " is: " .. result); - - x:int = 0; - until x > 9 { - println("" .. x); - x = x + 1 - } - - yield 0; -} \ No newline at end of file + 0 +}