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>, ()> {
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<Option<Data>, ()> {
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(())
}
}

View File

@ -12,6 +12,12 @@ pub fn println(args: &[Data]) -> Result<Option<Data>, ()> {
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> {
vec![
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),
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(prog) = vmrt::compile(&funcs, &declrs, builtin, &settings) {
println!("{}", diagnostics);
if let Ok(exit_code) = vmrt::execute(&prog) {
crate::message(MessageType::Info, format!("Program exited with {}", exit_code));
}
} else {
println!("{}", diagnostics);
}
} else {
println!("{}", diagnostics);
}
} else {
println!("{}", diagnostics);
}
println!("{}", diagnostics);
continue;
}

View File

@ -147,7 +147,7 @@ pub struct 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 {
name: Some(name),
args: if args.is_empty() {

View File

@ -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

View File

@ -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<i64, ()> {
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(

View File

@ -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

View File

@ -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;
0
}