work in progress for different types

parent 41e6f4de
......@@ -17,13 +17,67 @@ version = "0.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
version = "1.1.0"
name = "num"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"num-bigint 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num-complex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
"num-iter 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
"num-rational 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-bigint"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-complex"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-integer"
version = "0.1.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-iter"
version = "0.1.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-rational"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-bigint 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-traits"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "plex"
version = "0.2.3"
......@@ -40,7 +94,7 @@ dependencies = [
name = "prakda-compiler"
version = "0.1.0"
dependencies = [
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"plex 0.2.3 (git+https://github.com/goffrie/plex)",
"text_io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
......@@ -95,16 +149,17 @@ name = "vec_map"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum bit-set 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9bf6104718e80d7b26a68fdbacff3481cfc05df670821affc7e9cbc1884400c"
"checksum bit-vec 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "02b4ff8b16e6076c3e14220b39fbc1fabb6737522281a388998046859400895f"
"checksum lalr 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "106d7548f95adbe3019b4fc4954554d7b72535867aa9ce326d2f766b68958de7"
"checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7"
"checksum num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cf4825417e1e1406b3782a8ce92f4d53f26ec055e3622e1881ca8e9f5f9e08db"
"checksum num-bigint 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3eceac7784c5dc97c2d6edf30259b4e153e6e2b42b3c85e9a6e9f45d06caef6e"
"checksum num-complex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "107b9be86cd2481930688277b675b0114578227f034674726605b8a482d8baf8"
"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
"checksum num-iter 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "af3fdbbc3291a5464dc57b03860ec37ca6bf915ed6ee385e7c6c052c422b2124"
"checksum num-rational 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4e96f040177bb3da242b5b1ecf3f54b5d5af3efbbfb18608977a5d2767b22f10"
"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
"checksum plex 0.2.3 (git+https://github.com/goffrie/plex)" = "<none>"
"checksum proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)" = "3d7b7eaaa90b4a90a932a9ea6666c95a389e424eff347f0f793979289429feee"
"checksum quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "dd636425967c33af890042c483632d33fa7a18f19ad1d7ea72e8998c6ef8dea5"
......@@ -113,4 +168,3 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum text_io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9658b61ebd1d2a40c276ba2335890b9eb6550b67458a6fbce2022e58c3350a50"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cac5efe5cb0fa14ec2f84f83c701c562ee63f6dcc680861b21d65c682adfb05f"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
......@@ -6,5 +6,5 @@ edition = "2018"
[dependencies]
text_io = "0.1.7"
lazy_static = "1.1.0"
num = "0.2.0"
plex = {git = "https://github.com/goffrie/plex"}
......@@ -2,6 +2,7 @@
extern crate plex;
#[macro_use]
extern crate text_io;
extern crate num;
use std::collections::HashMap;
......@@ -16,21 +17,28 @@ mod lexer {
Make, // make
Exit, // exit // TODO: make it interpreter only
Integer(i32), // literal integer
// Float(f32), // literal float
Equals, // =
Plus, // +
Minus, // -
Star, // *
Slash, // /
LParen, // (
RParen, // )
Semi, // ;
Assign, // <-
Fargs, // :
Whitespace, // self-described
Comment, // /* */ w/o nest
Integer(i32), // literal integer
Uintegrer(u32), // literal unsigned integer
Float(f32), // literal float
Char(i8), // char ('c')
Uchar(u8), // unsigned char ('c') (explicit)
Equals, // =
Plus, // +
Minus, // -
Star, // *
Slash, // /
LParen, // (
RParen, // )
Semi, // ;
Assign, // <-
Fargs, // :
Return, // ->
LCbrac, // {
RCbrac, // }
Ampers, // &
Whitespace, // whitespace, duh
Comment, // $$$ ... $$$ w/o nest or $ ...
}
lexer! {
......@@ -39,26 +47,26 @@ mod lexer {
r#"[ \t\r\n]+"# => (Token::Whitespace, text),
// comments ///////////////////////////////////////////////////////////
// "C-style" comments (/* .. */) - can't contain "*/"
r#"/[*](~(.*[*]/.*))[*]/"# => (Token::Comment, text),
// "C++-style" comments (// ...)
r#"//[^\n]*"# => (Token::Comment, text),
// Multiline comments $$$ ... $$$
r#"\$\$\$^((\$\$\$).)*\$\$\$"# => (Token::Comment, text),
// Single line comments $ ...
r#"\$[^\n]*"# => (Token::Comment, text),
// native keywords ////////////////////////////////////////////////////
r#"print"# => (Token::Print, text), // `print`
r#"make"# => (Token::Make, text), // `make`
r#"exit"# => (Token::Exit, text), // `exit`
// numbers ////////////////////////////////////////////////////////////
// detects a litteral integer
// can end with an `i`
// r#"[0-9]+\.[0-9]+"# => {
// (if let Ok(i) = text.parse::<f32>() {
// Token::Float(i)
// } else {
// panic!("float {} is out of range", text)
// }, text)
// }
r#"make"# => (Token::Make, text), // `make`
r#"exit"# => (Token::Exit, text), // `exit`
// built-in types /////////////////////////////////////////////////////
// 0.0
r#"[0-9]+\.[0-9]+"# => {
(if let Ok(i) = text.parse::<f32>() {
Token::Float(i)
} else {
panic!("Float {} is out of range", text)
}, text)
}
// 0
r#"[0-9]+"# => {
(if let Ok(i) = text.parse::<i32>() {
Token::Integer(i)
......@@ -66,6 +74,46 @@ mod lexer {
panic!("integer {} is out of range", text)
}, text)
}
// 0
r#"[0-9]+u"# => {
(if let Ok(i) = text.parse::<u32>() {
Token::Uintegrer(i)
} else {
panic!("integer {} is out of range", text)
}, text)
}
// 'a'
r#"'[a-zA-Z]'"# => {
(if let Ok(i) = text.parse::<i8>() {
Token::Char(i)
} else {
panic!("char {} is out of range", text)
}, text)
}
// 97c
r#"[0-9]+c"# => {
(if let Ok(i) = text.parse::<i8>() {
Token::Char(i)
} else {
panic!("char {} is out of range", text)
}, text)
}
// 'a'u
r#"'[a-zA-Z]'uc"# => {
(if let Ok(i) = text.parse::<u8>() {
Token::Uchar(i)
} else {
panic!("char {} is out of range", text)
}, text)
}
// 97uc
r#"[0-9]+uc"# => {
(if let Ok(i) = text.parse::<u8>() {
Token::Uchar(i)
} else {
panic!("char {} is out of range", text)
}, text)
}
// strings ////////////////////////////////////////////////////////////
// detects a string
......@@ -73,15 +121,19 @@ mod lexer {
// symbols ////////////////////////////////////////////////////////////
r#"<-"# => (Token::Assign, text), // <-
r#":"# => (Token::Fargs, text), // :
r#"="# => (Token::Equals, text), // =
r#"\+"# => (Token::Plus, text), // +
r#"-"# => (Token::Minus, text), // -
r#"\*"# => (Token::Star, text), // *
r#"/"# => (Token::Slash, text), // /
r#"->"# => (Token::Return, text), // ->
r#"="# => (Token::Equals, text), // =
r#"\+"# => (Token::Plus, text), // +
r#"-"# => (Token::Minus, text), // -
r#"\*"# => (Token::Star, text), // *
r#"/"# => (Token::Slash, text), // /
r#"\("# => (Token::LParen, text), // (
r#"\)"# => (Token::RParen, text), // )
r#";"# => (Token::Semi, text), // ;
r#"\{"# => (Token::LCbrac, text), // {
r#"\}"# => (Token::RCbrac, text), // }
r#";"# => (Token::Semi, text), // ;
r#":"# => (Token::Fargs, text), // :
r#"\&"# => (Token::Ampers, text), // &
// if any other char //////////////////////////////////////////////////
r#"."# => panic!("unexpected character: {}", text),
......@@ -165,7 +217,7 @@ mod ast {
Assign(String, Box<Expr>),
Print(Box<Expr>),
LiteralInt(i32),
// LiteralFloat(f32),
LiteralFloat(f32),
#[allow(dead_code)]
Exit,
}
......@@ -249,10 +301,10 @@ mod parser {
span: span!(),
node: Expr_::LiteralInt(i),
},
// Float(i) => Expr {
// span: span!(),
// node: Expr_::LiteralFloat(i),
// },
Float(i) => Expr {
span: span!(),
node: Expr_::LiteralFloat(i),
},
LParen assign[a] RParen => a
}
}
......@@ -264,16 +316,24 @@ mod parser {
}
}
trait Number: num::Float + num::Integer {}
impl<T> Number for T where T: num::Float + num::Integer {}
mod interp {
use crate::ast::*;
use crate::Number;
use std::collections::HashMap;
pub fn interp(p: Program, mut env: &mut HashMap<String, i32>) {
pub fn interp<P: Number>(p: Program, mut env: &mut HashMap<String, P>) {
for expr in &p.stmts {
interp_expr(&mut env, expr);
}
}
fn interp_expr(env: &mut HashMap<String, i32>, expr: &Expr) -> i32 {
fn interp_expr<P>(env: &mut HashMap<String, P>, expr: &Expr) -> P
where
P: Number,
{
use crate::ast::Expr_::*;
match expr.node {
Add(ref a, ref b) => interp_expr(env, a) + interp_expr(env, b),
......@@ -287,12 +347,12 @@ mod interp {
}
Var(ref var) => *env.get(&var[..]).unwrap(),
LiteralInt(lit) => lit,
// LiteralFloat(lit) => lit,
LiteralFloat(lit) => lit,
Print(ref e) => {
let val = interp_expr(env, e);
println!("{}", val);
val
},
}
Exit => {
println!("Goodbye!");
::std::process::exit(0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment