Remove the "any" type

This commit is contained in:
0x4261756D 2022-12-31 16:09:55 +01:00
parent e3a1223ac9
commit 41667ff4a3
10 changed files with 54 additions and 35 deletions

View File

@ -1,7 +1,6 @@
use core::panic; use core::panic;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::fmt::format;
use std::fs; use std::fs;
use std::iter::Peekable; use std::iter::Peekable;
use std::process::exit; use std::process::exit;
@ -24,25 +23,25 @@ enum TokenizerState
Comment, Comment,
} }
#[derive(Debug,Clone,Copy)] #[derive(Debug,Clone,Copy, PartialEq)]
enum Datatype enum Datatype
{ {
Int, Int,
String, String,
Bool, Bool,
//Pointer, //Pointer,
Any, // Any,
} }
impl PartialEq for Datatype // impl PartialEq for Datatype
{ // {
fn eq(&self, other: &Self) -> bool // fn eq(&self, other: &Self) -> bool
{ // {
core::mem::discriminant(self) == core::mem::discriminant(&Datatype::Any) || // core::mem::discriminant(self) == core::mem::discriminant(&Datatype::Any) ||
core::mem::discriminant(other) == core::mem::discriminant(&Datatype::Any) || // core::mem::discriminant(other) == core::mem::discriminant(&Datatype::Any) ||
core::mem::discriminant(self) == core::mem::discriminant(other) // core::mem::discriminant(self) == core::mem::discriminant(other)
} // }
} // }
#[derive(Debug)] #[derive(Debug)]
struct Function struct Function
@ -84,8 +83,9 @@ fn main()
{ {
let intrinsics: HashMap<&str, (Vec<Datatype>, Vec<Datatype>)> = HashMap::from( let intrinsics: HashMap<&str, (Vec<Datatype>, Vec<Datatype>)> = HashMap::from(
[ [
("print", (Vec::from([Datatype::Any]), Vec::new())), ("print", (Vec::from([Datatype::String]), Vec::new())),
("println", (Vec::from([Datatype::Any]), Vec::new())), ("println", (Vec::from([Datatype::String]), Vec::new())),
("intToStr", (Vec::from([Datatype::Int]), Vec::from([Datatype::String]))),
("-", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Int]))), ("-", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Int]))),
("+", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Int]))), ("+", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Int]))),
("*", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Int]))), ("*", (Vec::from([Datatype::Int, Datatype::Int]), Vec::from([Datatype::Int]))),
@ -494,6 +494,11 @@ fn interpret_program(operations: &Vec<Operation>, queue: &mut Vec<String>, funct
{ {
output += format!("{}\n", queue.remove(0)).as_str(); output += format!("{}\n", queue.remove(0)).as_str();
} }
"intToStr" =>
{
let val = queue.remove(0).clone();
queue.push(val);
}
_ => _ =>
{ {
return Err(format!("Unexpected intrinsic '{}' at {}:{}", intrinsic_name, line, col)); return Err(format!("Unexpected intrinsic '{}' at {}:{}", intrinsic_name, line, col));
@ -948,7 +953,7 @@ fn extract_arrays(tokens: &mut Vec<Token>, intrinsics: &HashMap<&str, (Vec<Datat
let mut data: Vec<String> = Vec::new(); let mut data: Vec<String> = Vec::new();
let default_val = match datatype let default_val = match datatype
{ {
Datatype::Any | Datatype::String => String::new(), Datatype::String => String::new(),
Datatype::Bool => String::from("false"), Datatype::Bool => String::from("false"),
Datatype::Int => String::from("0"), Datatype::Int => String::from("0"),
}; };
@ -988,7 +993,7 @@ fn str_to_datatype(s: &str, line: i32, col: i32) -> Result<Datatype, String>
{ {
match s match s
{ {
"any" => Ok(Datatype::Any), //"any" => Ok(Datatype::Any),
"bool" => Ok(Datatype::Bool), "bool" => Ok(Datatype::Bool),
"int" => Ok(Datatype::Int), "int" => Ok(Datatype::Int),
"str" => Ok(Datatype::String), "str" => Ok(Datatype::String),
@ -1034,7 +1039,7 @@ fn extract_functions(tokens: &mut Vec<Token>, intrinsics: &HashMap<&str, (Vec<Da
} }
match word.as_str() match word.as_str()
{ {
"any" => ins.push(Datatype::Any), //"any" => ins.push(Datatype::Any),
"str" => ins.push(Datatype::String), "str" => ins.push(Datatype::String),
"int" => ins.push(Datatype::Int), "int" => ins.push(Datatype::Int),
"bool" => ins.push(Datatype::Bool), "bool" => ins.push(Datatype::Bool),
@ -1069,7 +1074,7 @@ fn extract_functions(tokens: &mut Vec<Token>, intrinsics: &HashMap<&str, (Vec<Da
{ {
match word.as_str() match word.as_str()
{ {
"any" => outs.push(Datatype::Any), //"any" => outs.push(Datatype::Any),
"str" => outs.push(Datatype::String), "str" => outs.push(Datatype::String),
"int" => outs.push(Datatype::Int), "int" => outs.push(Datatype::Int),
"bool" => outs.push(Datatype::Bool), "bool" => outs.push(Datatype::Bool),

View File

@ -10,7 +10,8 @@ function => dump
true 0 true 0
while while
{ {
dup test.read req print // i
dup test.read req intToStr req print
1 + test.length dup < req 1 + test.length dup < req
} }
deq "" println deq "" println

View File

@ -1,8 +1,8 @@
//invalid,Function name print at 3:22 is already an intrinsic:END: //invalid,Function name print at 3:22 is already an intrinsic:END:
function int => print function str => print
{ {
deq deq
} }
42 print "42" print

View File

@ -1,8 +1,8 @@
//invalid,Expected function name but got deq at 3:20:END: //invalid,Expected function name but got deq at 3:20:END:
function int => deq function str => deq
{ {
deq deq
} }
42 print "42" print

View File

@ -1,8 +1,8 @@
//invalid,Expected function name but got { at 4:2:END: //invalid,Expected function name but got { at 4:2:END:
function int => function str =>
{ {
deq deq
} }
42 print "42" print

View File

@ -14,4 +14,4 @@ function int int int int => int fibonacci
} }
} }
20 0 1 0 fibonacci println 20 0 1 0 fibonacci intToStr println

View File

@ -4,5 +4,5 @@
function int => int req_impl { } function int => int req_impl { }
1 2 3 req_impl print print println 1 2 3 req_impl intToStr intToStr intToStr print print println
1 2 3 req print print println 1 2 3 req intToStr intToStr intToStr print print println

View File

@ -5,9 +5,9 @@
// Dequeues, enqueues 42 and 17, prints the head // Dequeues, enqueues 42 and 17, prints the head
function any => int foo function int => int foo
{ {
deq 42 17 print deq 42 17 intToStr req print
} }
"test2" print false "test2" print false

View File

@ -1,9 +1,9 @@
//valid,42footesttest2stuff //valid,42footesttest2stuff
//:END: //:END:
function int str any => str str str foo function int str str => str str str foo
{ {
print req deq "test" "test2" "stuff" print intToStr req req print req deq "test" "test2" "stuff" print
} }
42 "foo" "bar" foo print print println 42 "foo" "bar" foo print print println

View File

@ -7,16 +7,29 @@ true while
} }
10 0 dup > req 10 0 dup > req
// true 10
while while
{ {
dup print // i
dup intToStr req print
1 - 0 dup > req 1 - 0 dup > req
} }
deq deq
function bool => str boolToStr
{
if
{
"true"
}
else
{
"false"
}
}
true true true while true true true while
{ {
false false
} }
print println boolToStr boolToStr print println