Change scope implementation to support values::Value

This commit is contained in:
Sameer Rahmani 2020-09-13 22:06:05 +01:00
parent 842309e035
commit f880169e68
5 changed files with 120 additions and 50 deletions

106
Cargo.lock generated
View File

@ -126,6 +126,17 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "getrandom"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "heck"
version = "0.3.1"
@ -271,7 +282,33 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12"
dependencies = [
"phf_macros",
"phf_shared",
"proc-macro-hack",
]
[[package]]
name = "phf_generator"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17367f0cc86f2d25802b2c26ee58a7b23faeccf78a396094c13dced0d0182526"
dependencies = [
"phf_shared",
"rand",
]
[[package]]
name = "phf_macros"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f6fde18ff429ffc8fe78e2bf7f8b7a5a5a6e2a8b58bc5a9ac69198bbda9189c"
dependencies = [
"phf_generator",
"phf_shared",
"proc-macro-hack",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@ -283,6 +320,12 @@ dependencies = [
"siphasher",
]
[[package]]
name = "ppv-lite86"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20"
[[package]]
name = "proc-macro-error"
version = "0.4.12"
@ -309,6 +352,12 @@ dependencies = [
"version_check",
]
[[package]]
name = "proc-macro-hack"
version = "0.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99c605b9a0adc77b7211c6b1f722dcb613d68d66859a44f3d485a6da332b0598"
[[package]]
name = "proc-macro2"
version = "1.0.18"
@ -327,6 +376,57 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom",
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
"rand_pcg",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
]
[[package]]
name = "rand_pcg"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429"
dependencies = [
"rand_core",
]
[[package]]
name = "redox_syscall"
version = "0.1.56"
@ -506,6 +606,12 @@ version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "winapi"
version = "0.3.8"

View File

@ -18,7 +18,7 @@ clap = { version = "3.0.0-beta.1", features = ["yaml"] }
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "llvm10-0" }
# Static compile time hashing generator
phf = { version = "0.8", default-features = false }
phf = { version = "0.8", features = ["macros"] }
[dev-dependencies]
rusty-hook = "^0.11.2"

View File

@ -17,13 +17,3 @@
pub mod def;
pub use self::def::def;
use crate::ast::Expr;
use crate::compiler::Compiler;
use crate::types::list::List;
use phf::phf_map;
pub type BuiltInFn = Fn(&Compiler, &List<Expr>) -> ExprResult;
pub static BUILTINS: phf::Map<&'static str, BuiltInFn> = phf_map! {
"def" => &def
};

View File

@ -18,7 +18,6 @@ use crate::ast::Expr;
use crate::compiler::Compiler;
use crate::types::{ExprResult, List};
pub fn def<'ctx>(compiler: &'ctx Compiler, args: &'ctx List<Expr>) -> ExprResult<'ctx> {
//compiler.current_ns().ins
Ok(())
pub fn def<'a>(compiler: &'a Compiler, args: &'a List<Expr>) -> ExprResult<'a> {
Err("Not implemented".to_string())
}

View File

@ -14,39 +14,16 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::builtins::{BuiltInFn, BUILTINS};
use crate::values;
use crate::values::Value;
use std::collections::HashMap;
/// This enum describes the type of the values in the scope.
/// Most values are LLVMValue's which are just some LLVM IR
/// representation.
/// But sometimes we're going to have lookups for build in symbols
/// like `def`, `if`, `fn` and so on.
pub enum ScopeElementType<'a> {
Value(values::Value<'a>),
BuiltinCall(BuiltInFn),
}
/// This struct describes the values in the scope.
struct ScopeElement<'a> {
element_type: ScopeElementType<'a>,
pub struct ScopeElement<'a> {
element_type: Value<'a>,
public: bool,
}
/// Lookup the given key `k` in the builtins and return a
/// ScopeElement from it
fn builtin_lookup<'a>(k: &'a str) -> Option<ScopeElement<'a>> {
match BUILTINS.get(k) {
Some(v) => Some(ScopeElement {
element_type: ScopeElementType::BuiltinCall(v),
public: true,
}),
None => None,
}
}
/// Scopes in **Serene** are simply represented by hashmaps. Each
/// Scope optionally has a parent scope that lookups fallback to
/// if the lookup key is missing from the current scope.
@ -70,23 +47,21 @@ impl<'a> Scope<'a> {
/// Lookup the given `key` in the scope and if it is not in the current
/// scope look it up in the `parent` scope.
pub fn lookup(&self, key: &'a str) -> Option<ScopeElement<'a>> {
let v = self.symbol_table.get(key);
if let None = v {
return match &self.parent {
pub fn lookup(&self, key: &'a str) -> Option<&ScopeElement<'a>> {
if self.symbol_table.contains_key(key) {
self.symbol_table.get(key)
} else {
match &self.parent {
Some(x) => x.lookup(key),
None => builtin_lookup(key),
};
None => None,
}
}
v.map(|x| *x)
}
pub fn insert(&mut self, key: &str, val: Value<'a>, public: bool) {
let v = ScopeElement {
public,
element_type: ScopeElementType::Value(val),
element_type: val,
};
self.symbol_table.insert(key.to_string(), v);
}