Compare commits
No commits in common. "cf1a5c1c3b6f458a5204d5ebfd40b79801439e8b" and "853021e4f8e392d6f8bd3e4ef76846fcdf180447" have entirely different histories.
cf1a5c1c3b
...
853021e4f8
|
@ -4,7 +4,7 @@ use std::fmt::Write;
|
|||
|
||||
use super::parser;
|
||||
use crate::rtlil;
|
||||
pub use callable::{Callable, CallableContext, CallableId};
|
||||
pub use callable::Callable;
|
||||
pub use types::{Type, TypeStruct, TypingContext};
|
||||
|
||||
mod callable;
|
||||
|
@ -64,8 +64,7 @@ impl Signal {
|
|||
|
||||
pub struct Context {
|
||||
/// map callable name to callable
|
||||
callable_names: BTreeMap<String, CallableId>,
|
||||
callables: CallableContext,
|
||||
callables: BTreeMap<String, Callable>,
|
||||
/// type names
|
||||
typenames: BTreeMap<String, Type>,
|
||||
types: TypingContext,
|
||||
|
@ -90,23 +89,16 @@ impl Counter {
|
|||
|
||||
impl Context {
|
||||
pub fn new() -> Self {
|
||||
let mut tcx = TypingContext::new();
|
||||
let ccx = CallableContext::new(&mut tcx);
|
||||
|
||||
let typenames = [
|
||||
let tcx = TypingContext::new();
|
||||
Context {
|
||||
callables: BTreeMap::new(),
|
||||
signals: BTreeMap::new(),
|
||||
types: TypingContext::new(),
|
||||
typenames: [
|
||||
("Logic".to_string(), tcx.primitives.logic),
|
||||
("Num".to_string(), tcx.primitives.elabnum),
|
||||
]
|
||||
.into();
|
||||
|
||||
let callable_names = [("reduce_or".to_string(), ccx.builtins.reduce_or)].into();
|
||||
|
||||
Context {
|
||||
callables: ccx,
|
||||
callable_names,
|
||||
signals: BTreeMap::new(),
|
||||
types: tcx,
|
||||
typenames,
|
||||
.into(),
|
||||
ids: Counter::new(),
|
||||
}
|
||||
}
|
||||
|
@ -123,8 +115,8 @@ impl Context {
|
|||
})
|
||||
}
|
||||
|
||||
fn try_get_callable(&self, callname: &str) -> Result<CallableId, CompileError> {
|
||||
self.callable_names.get(callname).copied().ok_or_else(|| {
|
||||
fn try_get_callable(&self, callname: &str) -> Result<&Callable, CompileError> {
|
||||
self.callables.get(callname).ok_or_else(|| {
|
||||
CompileError::new(CompileErrorKind::UndefinedReference(callname.to_owned()))
|
||||
})
|
||||
}
|
||||
|
@ -172,7 +164,7 @@ impl Context {
|
|||
typed_ir::Expr {
|
||||
id,
|
||||
kind: typed_ir::ExprKind::Call {
|
||||
called: self.callables.builtins.bitnot,
|
||||
called: typed_ir::DefId(99),
|
||||
args: vec![a],
|
||||
},
|
||||
typ: self.types.primitives.infer,
|
||||
|
@ -183,7 +175,7 @@ impl Context {
|
|||
typed_ir::Expr {
|
||||
id,
|
||||
kind: typed_ir::ExprKind::Call {
|
||||
called: self.callables.builtins.xor,
|
||||
called: typed_ir::DefId(99),
|
||||
args: vec![a, b],
|
||||
},
|
||||
typ: self.types.primitives.infer,
|
||||
|
@ -195,11 +187,10 @@ impl Context {
|
|||
.iter()
|
||||
.map(|expr| self.type_expression(expr))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let called = self.try_get_callable(call.name.fragment())?;
|
||||
typed_ir::Expr {
|
||||
id,
|
||||
kind: typed_ir::ExprKind::Call {
|
||||
called,
|
||||
called: typed_ir::DefId(99),
|
||||
args: args_resolved,
|
||||
},
|
||||
typ: self.types.primitives.infer,
|
||||
|
@ -293,8 +284,7 @@ impl Context {
|
|||
Ok(format!("_{}", arg.id.0))
|
||||
})
|
||||
.collect::<Result<Vec<_>, std::fmt::Error>>()?;
|
||||
let callable = self.callables.get(*called);
|
||||
format!("{}({})", callable.name(), args.join(", "))
|
||||
format!("_{}({})", called.0, args.join(", "))
|
||||
}
|
||||
};
|
||||
let mut type_pretty = String::new();
|
||||
|
|
|
@ -1,8 +1,4 @@
|
|||
use super::types::{Type, TypingContext};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq, Ord)]
|
||||
pub struct CallableId(pub usize);
|
||||
use super::types::Type;
|
||||
|
||||
pub struct Callable {
|
||||
pub name: String,
|
||||
|
@ -19,54 +15,3 @@ impl<'ty> Callable {
|
|||
self.args.len()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BuiltinCallables {
|
||||
pub xor: CallableId,
|
||||
pub bitnot: CallableId,
|
||||
pub reduce_or: CallableId,
|
||||
}
|
||||
|
||||
pub struct CallableContext {
|
||||
pub builtins: BuiltinCallables,
|
||||
callables: Vec<Callable>,
|
||||
}
|
||||
|
||||
impl CallableContext {
|
||||
pub fn new(typectx: &mut TypingContext) -> Self {
|
||||
let builtins = BuiltinCallables {
|
||||
xor: CallableId(0),
|
||||
bitnot: CallableId(1),
|
||||
reduce_or: CallableId(2),
|
||||
};
|
||||
Self {
|
||||
callables: vec![
|
||||
Callable {
|
||||
name: "builtin::xor".to_string(),
|
||||
args: vec![],
|
||||
ret_type: Some(typectx.primitives.logic),
|
||||
},
|
||||
Callable {
|
||||
name: "builtin::bitnot".to_string(),
|
||||
args: vec![],
|
||||
ret_type: Some(typectx.primitives.logic),
|
||||
},
|
||||
Callable {
|
||||
name: "builtin::reduce_or".to_string(),
|
||||
args: vec![],
|
||||
ret_type: Some(typectx.primitives.logic),
|
||||
},
|
||||
],
|
||||
builtins,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(&mut self, callable: Callable) -> CallableId {
|
||||
let id = self.callables.len();
|
||||
self.callables.push(callable);
|
||||
CallableId(id)
|
||||
}
|
||||
|
||||
pub fn get(&self, id: CallableId) -> &Callable {
|
||||
&self.callables[id.0]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use super::callable::CallableId;
|
||||
use super::types::{ElabData, Type};
|
||||
use std::fmt::Debug;
|
||||
|
||||
|
@ -32,7 +31,7 @@ pub struct Expr {
|
|||
pub enum ExprKind {
|
||||
Literal(ElabData),
|
||||
Path(DefId),
|
||||
Call { called: CallableId, args: Vec<Expr> },
|
||||
Call { called: DefId, args: Vec<Expr> },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
|
@ -27,17 +27,11 @@ enum TypeKind {
|
|||
/// UInt of generic width
|
||||
UInt(ElabData),
|
||||
/// Callable
|
||||
Callable(FnSig),
|
||||
Callable,
|
||||
/// A type that was not given and needs to be inferred
|
||||
Infer,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FnSig {
|
||||
params: Vec<Type>,
|
||||
ret: Type,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ElabData {
|
||||
typ: Type,
|
||||
|
@ -151,7 +145,7 @@ impl TypingContext {
|
|||
}
|
||||
}
|
||||
TypeKind::UInt(_) => todo!(),
|
||||
TypeKind::Callable(_sig) => todo!("callable generic params"),
|
||||
TypeKind::Callable => todo!("callable generic params"),
|
||||
// need to know what the type is to parameterize it
|
||||
TypeKind::Infer => None,
|
||||
}
|
||||
|
@ -178,7 +172,7 @@ impl TypingContext {
|
|||
}
|
||||
TypeKind::Infer => write!(w, "?"),
|
||||
TypeKind::UInt(_) => todo!("print uint"),
|
||||
TypeKind::Callable(_sig) => todo!("print callable"),
|
||||
TypeKind::Callable => todo!("print callable"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue