Improve scope to support builtins
This commit is contained in:
parent
b6a960862c
commit
ee47410d49
|
@ -265,6 +265,24 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "phf"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12"
|
||||
dependencies = [
|
||||
"phf_shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "phf_shared"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7"
|
||||
dependencies = [
|
||||
"siphasher",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
version = "0.4.12"
|
||||
|
@ -378,9 +396,16 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"clap",
|
||||
"inkwell",
|
||||
"phf",
|
||||
"rusty-hook",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "siphasher"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa8f3741c7372e75519bd9346068370c9cdaabcc1f9599cbcf2a2719352286b7"
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.4.0"
|
||||
|
|
|
@ -11,8 +11,14 @@ license-file = "LICENSE"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
# CLI library
|
||||
clap = { version = "3.0.0-beta.1", features = ["yaml"] }
|
||||
|
||||
# LLVM Binding
|
||||
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "llvm10-0" }
|
||||
|
||||
# Static compile time hashing generator
|
||||
phf = { version = "0.8", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
rusty-hook = "^0.11.2"
|
|
@ -29,7 +29,7 @@ pub enum Expr {
|
|||
NoMatch,
|
||||
}
|
||||
|
||||
impl<'a> Expr {
|
||||
impl Expr {
|
||||
pub fn make_list(first: Expr, rest: Expr) -> Expr {
|
||||
Expr::Cons(List::<Expr>::new(Box::new(first), Box::new(rest)))
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
/** Serene --- Yet an other Lisp
|
||||
*
|
||||
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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
|
||||
};
|
|
@ -0,0 +1,24 @@
|
|||
/** Serene --- Yet an other Lisp
|
||||
*
|
||||
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* 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::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(())
|
||||
}
|
|
@ -104,9 +104,11 @@ pub fn create_context() -> Context {
|
|||
return Context::create();
|
||||
}
|
||||
|
||||
/// Compiles the given `ast` using the given `compiler` into
|
||||
/// LLVM IR.
|
||||
pub fn compile<'ctx>(
|
||||
compiler: &'ctx Compiler,
|
||||
exprs: Vec<impl Expression>,
|
||||
ast: Vec<impl Expression>,
|
||||
) -> Vec<Result<AnyValueEnum<'ctx>, String>> {
|
||||
match compiler.current_ns() {
|
||||
Some(ns) => ns,
|
||||
|
@ -115,7 +117,7 @@ pub fn compile<'ctx>(
|
|||
|
||||
let mut generated_code = vec![];
|
||||
|
||||
for expr in &exprs {
|
||||
for expr in &ast {
|
||||
generated_code.push(expr.code_gen(compiler));
|
||||
}
|
||||
|
||||
|
|
|
@ -23,11 +23,13 @@ use std::io::prelude::*;
|
|||
use std::string::String;
|
||||
|
||||
pub mod ast;
|
||||
pub mod builtins;
|
||||
pub mod compiler;
|
||||
pub mod namespace;
|
||||
pub mod reader;
|
||||
pub mod scope;
|
||||
pub mod types;
|
||||
pub mod values;
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let yaml = load_yaml!("cli.yml");
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
* 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::scope::Scope;
|
||||
use crate::scope::Scope;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::module::Module;
|
||||
use inkwell::values::FunctionValue;
|
||||
|
@ -24,7 +24,7 @@ pub struct Namespace<'ctx> {
|
|||
/// think of modules as compilation units. Object files if you prefer.
|
||||
/// This way we should be able to hot swap the namespaces.
|
||||
pub module: Module<'ctx>,
|
||||
//scope: Scope<'ctx>,
|
||||
scope: Scope<'ctx>,
|
||||
|
||||
// The option of the current function being compiled
|
||||
current_fn_opt: Option<FunctionValue<'ctx>>,
|
||||
|
@ -36,6 +36,7 @@ impl<'ctx> Namespace<'ctx> {
|
|||
module: context.create_module(&name),
|
||||
//scope: Scope::new(None),
|
||||
current_fn_opt: None,
|
||||
scope: Scope::new(None),
|
||||
}
|
||||
}
|
||||
/// Get a defined function given its name.
|
||||
|
|
47
src/scope.rs
47
src/scope.rs
|
@ -14,15 +14,45 @@
|
|||
* 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 inkwell::values::PointerValue;
|
||||
use crate::builtins::{BuiltInFn, BUILTINS};
|
||||
use crate::values;
|
||||
|
||||
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>,
|
||||
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.
|
||||
pub struct Scope<'a> {
|
||||
parent: Option<Box<Scope<'a>>>,
|
||||
symbol_table: HashMap<String, PointerValue<'a>>,
|
||||
symbol_table: HashMap<String, ScopeElement<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> Scope<'a> {
|
||||
|
@ -40,19 +70,24 @@ 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: &str) -> Option<PointerValue> {
|
||||
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 {
|
||||
Some(x) => x.lookup(key),
|
||||
None => None,
|
||||
None => builtin_lookup(key),
|
||||
};
|
||||
}
|
||||
|
||||
v.map(|x| *x)
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, key: &str, val: PointerValue<'a>) {
|
||||
self.symbol_table.insert(key.to_string(), val);
|
||||
pub fn insert(&mut self, key: &str, val: Value<'a>, public: bool) {
|
||||
let v = ScopeElement {
|
||||
public,
|
||||
element_type: ScopeElementType::Value(val),
|
||||
};
|
||||
self.symbol_table.insert(key.to_string(), v);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
/** Serene --- Yet an other Lisp
|
||||
*
|
||||
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* 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::ast::Expr;
|
||||
use inkwell::values::AnyValueEnum;
|
||||
|
||||
pub struct Value<'a> {
|
||||
llvm_id: Option<&'a str>,
|
||||
lllvm_value: AnyValueEnum<'a>,
|
||||
expr: &'a Expr,
|
||||
}
|
Loading…
Reference in New Issue