Remove the "any" type
This commit is contained in:
41
src/main.rs
41
src/main.rs
@ -1,7 +1,6 @@
|
||||
use core::panic;
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::fmt::format;
|
||||
use std::fs;
|
||||
use std::iter::Peekable;
|
||||
use std::process::exit;
|
||||
@ -24,25 +23,25 @@ enum TokenizerState
|
||||
Comment,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Copy)]
|
||||
#[derive(Debug,Clone,Copy, PartialEq)]
|
||||
enum Datatype
|
||||
{
|
||||
Int,
|
||||
String,
|
||||
Bool,
|
||||
//Pointer,
|
||||
Any,
|
||||
// Any,
|
||||
}
|
||||
|
||||
impl PartialEq for Datatype
|
||||
{
|
||||
fn eq(&self, other: &Self) -> bool
|
||||
{
|
||||
core::mem::discriminant(self) == core::mem::discriminant(&Datatype::Any) ||
|
||||
core::mem::discriminant(other) == core::mem::discriminant(&Datatype::Any) ||
|
||||
core::mem::discriminant(self) == core::mem::discriminant(other)
|
||||
}
|
||||
}
|
||||
// impl PartialEq for Datatype
|
||||
// {
|
||||
// fn eq(&self, other: &Self) -> bool
|
||||
// {
|
||||
// core::mem::discriminant(self) == core::mem::discriminant(&Datatype::Any) ||
|
||||
// core::mem::discriminant(other) == core::mem::discriminant(&Datatype::Any) ||
|
||||
// core::mem::discriminant(self) == core::mem::discriminant(other)
|
||||
// }
|
||||
// }
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Function
|
||||
@ -84,8 +83,9 @@ fn main()
|
||||
{
|
||||
let intrinsics: HashMap<&str, (Vec<Datatype>, Vec<Datatype>)> = HashMap::from(
|
||||
[
|
||||
("print", (Vec::from([Datatype::Any]), Vec::new())),
|
||||
("println", (Vec::from([Datatype::Any]), Vec::new())),
|
||||
("print", (Vec::from([Datatype::String]), 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]))),
|
||||
@ -494,6 +494,11 @@ fn interpret_program(operations: &Vec<Operation>, queue: &mut Vec<String>, funct
|
||||
{
|
||||
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));
|
||||
@ -948,7 +953,7 @@ fn extract_arrays(tokens: &mut Vec<Token>, intrinsics: &HashMap<&str, (Vec<Datat
|
||||
let mut data: Vec<String> = Vec::new();
|
||||
let default_val = match datatype
|
||||
{
|
||||
Datatype::Any | Datatype::String => String::new(),
|
||||
Datatype::String => String::new(),
|
||||
Datatype::Bool => String::from("false"),
|
||||
Datatype::Int => String::from("0"),
|
||||
};
|
||||
@ -988,7 +993,7 @@ fn str_to_datatype(s: &str, line: i32, col: i32) -> Result<Datatype, String>
|
||||
{
|
||||
match s
|
||||
{
|
||||
"any" => Ok(Datatype::Any),
|
||||
//"any" => Ok(Datatype::Any),
|
||||
"bool" => Ok(Datatype::Bool),
|
||||
"int" => Ok(Datatype::Int),
|
||||
"str" => Ok(Datatype::String),
|
||||
@ -1034,7 +1039,7 @@ fn extract_functions(tokens: &mut Vec<Token>, intrinsics: &HashMap<&str, (Vec<Da
|
||||
}
|
||||
match word.as_str()
|
||||
{
|
||||
"any" => ins.push(Datatype::Any),
|
||||
//"any" => ins.push(Datatype::Any),
|
||||
"str" => ins.push(Datatype::String),
|
||||
"int" => ins.push(Datatype::Int),
|
||||
"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()
|
||||
{
|
||||
"any" => outs.push(Datatype::Any),
|
||||
//"any" => outs.push(Datatype::Any),
|
||||
"str" => outs.push(Datatype::String),
|
||||
"int" => outs.push(Datatype::Int),
|
||||
"bool" => outs.push(Datatype::Bool),
|
||||
|
Reference in New Issue
Block a user