fixed minor issues

This commit is contained in:
Sven Vogel 2023-05-03 12:46:27 +02:00
parent 981a6688d4
commit 5b24bd80ac
8 changed files with 73 additions and 63 deletions

View File

@ -16,14 +16,16 @@ pub fn rat_to_int(args: &[Data]) -> Result<Option<Data>, ()> {
pub fn str_to_int(args: &[Data]) -> Result<Option<Data>, ()> { pub fn str_to_int(args: &[Data]) -> Result<Option<Data>, ()> {
match &args[0] { 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(()) _ => Err(())
} }
} }
pub fn str_to_rat(args: &[Data]) -> Result<Option<Data>, ()> { pub fn str_to_rat(args: &[Data]) -> Result<Option<Data>, ()> {
match &args[0] { 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(()) _ => Err(())
} }
} }

View File

@ -12,6 +12,12 @@ pub fn println(args: &[Data]) -> Result<Option<Data>, ()> {
Ok(None) Ok(None)
} }
pub fn read_line(_: &[Data]) -> Result<Option<Data>, ()> {
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<crate::builtin::BuiltinFun> { pub fn get_module_funs<'a>() -> Vec<crate::builtin::BuiltinFun> {
vec![ vec![
BuiltinFun { BuiltinFun {
@ -22,5 +28,9 @@ pub fn get_module_funs<'a>() -> Vec<crate::builtin::BuiltinFun> {
declr: Declr::generate_builtin("println", vec![("text", Prim::Str)], None), declr: Declr::generate_builtin("println", vec![("text", Prim::Str)], None),
func: &println func: &println
}, },
BuiltinFun {
declr: Declr::generate_builtin("readline", vec![], Some(Prim::Str)),
func: &read_line
},
] ]
} }

View File

@ -40,14 +40,21 @@ fn compile(settings: &Settings) -> Option<(Vec<Func>, Vec<Declr>, Vec<BuiltinFun
if let Ok((funcs, declrs, builtin)) = parser.parse(&mut tokens, &mut diagnostics, &settings) { if let Ok((funcs, declrs, builtin)) = parser.parse(&mut tokens, &mut diagnostics, &settings) {
if let Ok(prog) = vmrt::compile(&funcs, &declrs, builtin, &settings) { if let Ok(prog) = vmrt::compile(&funcs, &declrs, builtin, &settings) {
println!("{}", diagnostics);
if let Ok(exit_code) = vmrt::execute(&prog) { if let Ok(exit_code) = vmrt::execute(&prog) {
crate::message(MessageType::Info, format!("Program exited with {}", exit_code)); crate::message(MessageType::Info, format!("Program exited with {}", exit_code));
} }
} } else {
}
}
println!("{}", diagnostics); println!("{}", diagnostics);
}
} else {
println!("{}", diagnostics);
}
} else {
println!("{}", diagnostics);
}
continue; continue;
} }

View File

@ -147,7 +147,7 @@ pub struct Declr<'a> {
impl<'a> Declr<'a> { impl<'a> Declr<'a> {
pub fn generate_builtin(name: &'static str, args: Vec<(&'static str, Prim)>, ret: Option<Prim>) -> Declr { pub fn generate_builtin(name: &'static str, args: Vec<(&'static str, Prim)>, ret: Option<Prim>) -> Declr<'a> {
Declr { Declr {
name: Some(name), name: Some(name),
args: if args.is_empty() { args: if args.is_empty() {

View File

@ -150,7 +150,7 @@ impl Operator {
} }
for (x, typ) in types.iter().enumerate() { for (x, typ) in types.iter().enumerate() {
if *typ != operands[x] { if *typ != operands[operands.len() - x - 1] {
return Err(()); return Err(());
} }
} }
@ -596,8 +596,8 @@ const TOKEN_REGEX_SRC: &'static str = concat!(
r"|'([a-zA-Z0-9_]+)", // labels: 'example r"|'([a-zA-Z0-9_]+)", // labels: 'example
r"|(goto\s+[a-zA-Z0-9_]+", // goto example r"|(goto\s+[a-zA-Z0-9_]+", // goto example
r"|despite|until|loop|break|cont|ret|yield|please)", // keywords 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"|\b(int|rat|bool|str)\b", // raw data types
r"|(true|false|ye|no|maybe)", // boolean values 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_]+))?\s*=[^=]", // assignment var:int=
r"|([A-Za-z_]+)\s*(?::\s*([a-zA-Z0-9_]+))", // declaration var:int r"|([A-Za-z_]+)\s*(?::\s*([a-zA-Z0-9_]+))", // declaration var:int
r"|([A-Za-z_]+)", // symbol r"|([A-Za-z_]+)", // symbol

View File

@ -323,32 +323,44 @@ fn parse_term<'a>(
Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpEq)), Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpEq)),
Prim::Rat => Instr::Operation(Operation::Rat(RatOp::CmpEq)), Prim::Rat => Instr::Operation(Operation::Rat(RatOp::CmpEq)),
Prim::Bool => Instr::Operation(Operation::Bool(BoolOp::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!(), _ => panic!(),
}, },
crate::token::Operator::NotEq => match hint.unwrap() { crate::token::Operator::NotEq => match hint.unwrap() {
Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpNEq)), Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpNEq)),
Prim::Rat => Instr::Operation(Operation::Rat(RatOp::CmpNEq)), Prim::Rat => Instr::Operation(Operation::Rat(RatOp::CmpNEq)),
Prim::Bool => Instr::Operation(Operation::Bool(BoolOp::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!(), _ => panic!(),
}, },
crate::token::Operator::Lt => match hint.unwrap() { crate::token::Operator::Lt => match hint.unwrap() {
Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpLt)), Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpLt)),
Prim::Rat => Instr::Operation(Operation::Rat(RatOp::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!(), _ => panic!(),
}, },
crate::token::Operator::Gt => match hint.unwrap() { crate::token::Operator::Gt => match hint.unwrap() {
Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpGt)), Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpGt)),
Prim::Rat => Instr::Operation(Operation::Rat(RatOp::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!(), _ => panic!(),
}, },
crate::token::Operator::GtEq => match hint.unwrap() { crate::token::Operator::GtEq => match hint.unwrap() {
Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpGtEq)), Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpGtEq)),
Prim::Rat => Instr::Operation(Operation::Rat(RatOp::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!(), _ => panic!(),
}, },
crate::token::Operator::LtEq => match hint.unwrap() { crate::token::Operator::LtEq => match hint.unwrap() {
Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpLtEq)), Prim::Int => Instr::Operation(Operation::Int(IntOp::CmpLtEq)),
Prim::Rat => Instr::Operation(Operation::Rat(RatOp::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!(), _ => panic!(),
}, },
@ -742,7 +754,7 @@ pub fn execute(prog: &Program) -> Result<i64, ()> {
return Ok(exit_code.to_int()?); 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(()); return Err(());
} else { } else {
crate::message( crate::message(

View File

@ -4,6 +4,8 @@ extern function print
extern function println extern function println
extern function readline
extern function to_rat extern function to_rat
extern function to_int extern function to_int
@ -12,10 +14,22 @@ extern function parse_int
extern function parse_rat extern function parse_rat
extern function cos
extern function pow
extern function sin
extern function tan
extern function ln
extern function sqrt
fac: fac:
Load Arg x Load Arg x
Load Int 1 Load Int 2
NotEq Int Gt Num(Int)
Unless Unless
Load Int 1 Load Int 1
Yield Yield
@ -24,40 +38,18 @@ fac:
Sub Int Sub Int
Call fac Call fac
Load Arg x Load Arg x
Mul Int Load Int 2
Yield Sub Int
Call fac
number: Add Int
Load Rat 12.0
main: main:
Call number Load Int 24
Call to_int
Call fac Call fac
Store Int result Store Int c
Load String "The Factorial of " Load String "factorial: "
Call number Load Var c
Load String " is: "
Load Var result
Cat Str
Cat Str
Cat Str Cat Str
Call println Call println
Load Int 0 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

View File

@ -1,31 +1,18 @@
@feature(io, conv) @feature(io, conv, math)
@version(100) @version(100)
@author(Sven Vogel) @author(Sven Vogel)
-- compute the factorial max(a:int, b:int)=int {
-- in a recursive and functional way despite a < b {
fac(x:int) = int { yield a
despite x != 1 { }
yield 1; b
} }
yield fac(x - 1) * x # main function
}
number = rat 12.0 # this is a function without arguments and a braceless body
// main function
main = int { main = int {
result = fac(to_int(number)); println("" .. max(3,4));
println("The Factorial of " .. number() .. " is: " .. result); 0
x:int = 0;
until x > 9 {
println("" .. x);
x = x + 1
}
yield 0;
} }