@@ -1,11 +1,6 @@
|
||||
use crate::{
|
||||
compile::{ctx::Ctx, error::*, ir, visit::*},
|
||||
syn::{ast::*, op::BinOp, span::*},
|
||||
compile::{
|
||||
ctx::Ctx,
|
||||
error::*,
|
||||
ir,
|
||||
visit::*,
|
||||
},
|
||||
};
|
||||
|
||||
// basic block
|
||||
@@ -20,10 +15,7 @@ pub struct TranslateAst<'c, 't> {
|
||||
|
||||
impl<'c, 't> TranslateAst<'c, 't> {
|
||||
pub fn new(ctx: &'c mut Ctx, text: &'t str) -> Self {
|
||||
TranslateAst {
|
||||
ctx,
|
||||
text,
|
||||
}
|
||||
TranslateAst { ctx, text }
|
||||
}
|
||||
|
||||
pub fn translate(&mut self, ast: &Vec<Stmt>) -> Result<ir::Body> {
|
||||
@@ -33,13 +25,16 @@ impl<'c, 't> TranslateAst<'c, 't> {
|
||||
fn visit_lhs_expr(&mut self, expr: &Expr) -> Result<ir::Lhs> {
|
||||
match expr {
|
||||
Expr::Bin(b) if b.op == BinOp::Dot => todo!(),
|
||||
Expr::Base(BaseExpr { kind: BaseExprKind::Ident, .. }) => {
|
||||
Expr::Base(BaseExpr {
|
||||
kind: BaseExprKind::Ident,
|
||||
..
|
||||
}) => {
|
||||
let name = expr.text_at(self.text);
|
||||
//let name_id = self.ctx.
|
||||
todo!()
|
||||
todo!()
|
||||
//Ok(ir::Lhs::Name(
|
||||
}
|
||||
_ => todo!()
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
use crate::{
|
||||
compile::name::NameStack,
|
||||
obj::Sym,
|
||||
};
|
||||
use crate::{compile::name::NameStack, obj::Sym};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct Ctx {
|
||||
@@ -35,8 +32,6 @@ impl Ctx {
|
||||
|
||||
pub fn add_sym(&mut self, name: String) -> Sym {
|
||||
let next_sym = self.syms().len();
|
||||
*self.syms_mut()
|
||||
.entry(name)
|
||||
.or_insert(next_sym.into())
|
||||
*self.syms_mut().entry(name).or_insert(next_sym.into())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,7 @@ use snafu::Snafu;
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum Error {
|
||||
#[snafu(display("invalid assignment target"))]
|
||||
InvalidLhs {
|
||||
span: Span,
|
||||
}
|
||||
InvalidLhs { span: Span },
|
||||
}
|
||||
|
||||
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
use crate::{
|
||||
obj::Sym,
|
||||
syn::span::*,
|
||||
};
|
||||
use crate::{obj::Sym, syn::span::*};
|
||||
|
||||
pub type Body = Vec<Stmt>;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#[macro_use] pub mod visit;
|
||||
#[macro_use]
|
||||
pub mod visit;
|
||||
pub mod block;
|
||||
pub mod ctx;
|
||||
pub mod error;
|
||||
@@ -8,4 +9,3 @@ pub mod name;
|
||||
// * Desugar
|
||||
// * Collect names as symbols
|
||||
// * Create basic blocks
|
||||
|
||||
|
||||
@@ -11,8 +11,7 @@ pub trait Accept {
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
pub trait DefaultAccept<V: Visit<Self>>: Accept + Sized
|
||||
{
|
||||
pub trait DefaultAccept<V: Visit<Self>>: Accept + Sized {
|
||||
fn default_accept(&self, visitor: &mut V) -> V::Out;
|
||||
}
|
||||
|
||||
@@ -35,7 +34,7 @@ macro_rules! empty_visitor {
|
||||
type Out = ();
|
||||
fn visit(&mut self, _: &$default) -> Self::Out {}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_accept {
|
||||
|
||||
19
src/main.rs
19
src/main.rs
@@ -2,16 +2,12 @@
|
||||
#![feature(unsize, coerce_unsized, new_uninit)]
|
||||
|
||||
mod compile;
|
||||
mod syn;
|
||||
mod obj;
|
||||
mod mem;
|
||||
mod obj;
|
||||
mod syn;
|
||||
mod vm;
|
||||
|
||||
use std::{
|
||||
convert::TryFrom,
|
||||
fs,
|
||||
path::PathBuf,
|
||||
};
|
||||
use std::{convert::TryFrom, fs, path::PathBuf};
|
||||
use structopt::StructOpt;
|
||||
|
||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
@@ -24,7 +20,6 @@ struct Options {
|
||||
/// Supplying - for the path will read from STDIN.
|
||||
#[structopt(name = "FILE", parse(from_os_str))]
|
||||
input: PathBuf,
|
||||
|
||||
/*
|
||||
/// Disassemble object that would be passed to VM and exit before running it.
|
||||
#[structopt(short = "d", long)]
|
||||
@@ -33,16 +28,14 @@ struct Options {
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
use syn::{
|
||||
parser::Parser,
|
||||
};
|
||||
use syn::parser::Parser;
|
||||
|
||||
let opt = Options::from_args();
|
||||
let text = fs::read_to_string(&opt.input)?;
|
||||
let mut parser = Parser::try_from(text.as_str())?;
|
||||
let ast = parser.next_body()?;
|
||||
//println!("{:#?}", ast);
|
||||
|
||||
|
||||
let mut ctx = compile::ctx::Ctx::new();
|
||||
compile::name::CollectSyms::new(&mut ctx, text.as_str()).collect(&ast);
|
||||
println!("{:#?}", ctx.syms());
|
||||
@@ -54,6 +47,6 @@ fn main() -> Result<()> {
|
||||
}
|
||||
let names = ctx.name_stack_mut().pop().unwrap();
|
||||
//println!("{:#?}", names);
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
use crate::{
|
||||
mem::{
|
||||
gc::Gc,
|
||||
ptr::ObjCell,
|
||||
},
|
||||
mem::{gc::Gc, ptr::ObjCell},
|
||||
obj::prelude::*,
|
||||
};
|
||||
use std::{ptr::NonNull};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct BasicGc {
|
||||
@@ -55,7 +52,7 @@ impl Gc for BasicGc {
|
||||
#[test]
|
||||
fn test_gc_clone() {
|
||||
use crate::{obj::{attrs::AttrsBuilder, ctx::DefaultGc, Attrs, ObjCtx}};
|
||||
|
||||
|
||||
let mut ctx = ObjCtx::new(DefaultGc::default());
|
||||
|
||||
let empty_attrs = Attrs::new(&mut ctx, Default::default());
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
use crate::{
|
||||
obj::prelude::*,
|
||||
mem::{
|
||||
ptr::ObjCell,
|
||||
}
|
||||
};
|
||||
use crate::{mem::ptr::ObjCell, obj::prelude::*};
|
||||
|
||||
pub trait Gc {
|
||||
fn alloc<O: Obj + 'static>(&mut self, obj: O) -> ObjRef<O>;
|
||||
|
||||
@@ -3,13 +3,12 @@ pub mod gc;
|
||||
pub mod intern;
|
||||
pub mod ptr;
|
||||
|
||||
pub use basic::BasicGc;
|
||||
pub use basic::BasicGc;
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::mem::{
|
||||
BasicGc,
|
||||
gc::Gc,
|
||||
ptr::{DynRef, ObjRef},
|
||||
BasicGc,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,8 @@ use std::{
|
||||
pub type DynRef = ObjRef<dyn Obj>;
|
||||
|
||||
pub struct ObjRef<O>
|
||||
where O: Obj + ?Sized,
|
||||
where
|
||||
O: Obj + ?Sized,
|
||||
{
|
||||
ptr: NonNull<ObjCell<O>>,
|
||||
}
|
||||
@@ -126,14 +127,18 @@ where
|
||||
}
|
||||
|
||||
impl<T, U> CoerceUnsized<ObjRef<U>> for ObjRef<T>
|
||||
where T: Obj + Unsize<U> + ?Sized,
|
||||
U: Obj + ?Sized
|
||||
{}
|
||||
where
|
||||
T: Obj + Unsize<U> + ?Sized,
|
||||
U: Obj + ?Sized,
|
||||
{
|
||||
}
|
||||
|
||||
impl<T, U> CoerceUnsized<ObjCell<U>> for ObjCell<T>
|
||||
where T: CoerceUnsized<U> + Obj,
|
||||
U: Obj
|
||||
{}
|
||||
where
|
||||
T: CoerceUnsized<U> + Obj,
|
||||
U: Obj,
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// ObjCell
|
||||
|
||||
@@ -97,10 +97,7 @@ impl<'i, I: Intern> AttrsBuilder<'i, I> {
|
||||
}
|
||||
|
||||
pub fn with_base(intern: &'i mut I, attrs: Ss) -> Self {
|
||||
AttrsBuilder {
|
||||
attrs,
|
||||
intern,
|
||||
}
|
||||
AttrsBuilder { attrs, intern }
|
||||
}
|
||||
|
||||
pub fn attr(self, symbol_name: &str, value: DynRef) -> Self {
|
||||
@@ -125,7 +122,7 @@ fn test_attrs_new() {
|
||||
|
||||
let mut gc = BasicGc::default();
|
||||
let mut intern = BasicIntern::default();
|
||||
|
||||
|
||||
let attrs_ref = Attrs::new(&mut gc, Default::default());
|
||||
|
||||
{
|
||||
@@ -133,13 +130,10 @@ fn test_attrs_new() {
|
||||
let sym = intern.intern_sym("symbol");
|
||||
attrs.insert(sym, attrs_ref.as_dyn());
|
||||
|
||||
assert!(
|
||||
ptr::eq(
|
||||
dbg!(attrs.get(sym).unwrap().as_ptr()),
|
||||
dbg!(attrs_ref.as_dyn().as_ptr())
|
||||
// ^ as_dyn() is important here - this will cause the test to fail
|
||||
// otherwise
|
||||
),
|
||||
);
|
||||
assert!(ptr::eq(
|
||||
dbg!(attrs.get(sym).unwrap().as_ptr()),
|
||||
dbg!(attrs_ref.as_dyn().as_ptr()) // ^ as_dyn() is important here - this will cause the test to fail
|
||||
// otherwise
|
||||
),);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,7 @@ use snafu::Snafu;
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum Error {
|
||||
#[snafu(display("illegal attr key"))]
|
||||
AttrKey {
|
||||
key: DynRef
|
||||
}
|
||||
AttrKey { key: DynRef },
|
||||
}
|
||||
|
||||
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||
|
||||
|
||||
@@ -11,10 +11,7 @@ pub struct NativeFun {
|
||||
impl NativeFun {
|
||||
pub fn new(attrs: AttrsRef, name: StrRef, fn_ptr: NativeFunPtr) -> Self {
|
||||
// TODO : clone attrs, add name
|
||||
NativeFun {
|
||||
attrs,
|
||||
fn_ptr,
|
||||
}
|
||||
NativeFun { attrs, fn_ptr }
|
||||
}
|
||||
|
||||
pub fn call(&self, argv: Vec<DynRef>) {
|
||||
|
||||
@@ -6,7 +6,10 @@ pub mod fun;
|
||||
pub mod name;
|
||||
//pub mod num;
|
||||
pub mod ns {
|
||||
use crate::{mem::ptr::DynRef, obj::{NameId, Sym}};
|
||||
use crate::{
|
||||
mem::ptr::DynRef,
|
||||
obj::{NameId, Sym},
|
||||
};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// A namespace, indexed by `NameID`. This is used for scopes and local values.
|
||||
@@ -26,7 +29,7 @@ pub mod prelude {
|
||||
ptr::{DynRef, ObjRef},
|
||||
},
|
||||
obj::{
|
||||
Attrs, AttrsRef, Dict, DictRef, Fun, NativeFun, NameId, Ns, Obj, ObjCtx, Ss, Str,
|
||||
Attrs, AttrsRef, Dict, DictRef, Fun, NameId, NativeFun, Ns, Obj, ObjCtx, Ss, Str,
|
||||
StrRef, Sym,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -3,16 +3,8 @@ use derivative::Derivative;
|
||||
|
||||
pub mod prelude {
|
||||
pub use super::{
|
||||
Stmt,
|
||||
AssignStmt,
|
||||
Expr,
|
||||
BinExpr,
|
||||
AssignStmt, BaseExpr, BaseExprKind, BinExpr, Expr, FunCallExpr, FunExpr, IndexExpr, Stmt,
|
||||
UnExpr,
|
||||
FunCallExpr,
|
||||
IndexExpr,
|
||||
FunExpr,
|
||||
BaseExpr,
|
||||
BaseExprKind
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -91,10 +91,7 @@ impl<'t> Lexer<'t> {
|
||||
.ignore_whitespace(true)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
static ref SKIP_REGEX: Regex = Regex::new(
|
||||
r"^((#[^\n]*)\n?|[ \t\r]+)+"
|
||||
).unwrap();
|
||||
static ref SKIP_REGEX: Regex = Regex::new(r"^((#[^\n]*)\n?|[ \t\r]+)+").unwrap();
|
||||
}
|
||||
|
||||
const CAPTURES: &[(&str, TokenKind)] = &[
|
||||
@@ -139,13 +136,12 @@ impl<'t> Lexer<'t> {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
let caps =
|
||||
REGEX
|
||||
.captures(self.pos_text())
|
||||
.ok_or_else(|| Error::Unexpected {
|
||||
what: "EOF".to_string(),
|
||||
pos: self.pos,
|
||||
})?;
|
||||
let caps = REGEX
|
||||
.captures(self.pos_text())
|
||||
.ok_or_else(|| Error::Unexpected {
|
||||
what: "EOF".to_string(),
|
||||
pos: self.pos,
|
||||
})?;
|
||||
|
||||
// Get first capture
|
||||
let capture_kind = CAPTURES
|
||||
@@ -165,7 +161,6 @@ impl<'t> Lexer<'t> {
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
let start = self.pos;
|
||||
self.pos.adv_str(token_text);
|
||||
let end = self.pos;
|
||||
@@ -211,8 +206,10 @@ mod test {
|
||||
assert!(matches!(lexer.next_token(), Ok(None)));
|
||||
assert!(lexer.is_eof());
|
||||
|
||||
let mut lexer = Lexer::new(r"#comment
|
||||
#another comment");
|
||||
let mut lexer = Lexer::new(
|
||||
r"#comment
|
||||
#another comment",
|
||||
);
|
||||
assert!(matches!(lexer.next_token(), Ok(None)));
|
||||
assert!(lexer.is_eof());
|
||||
}
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
use crate::{
|
||||
obj::prelude::*,
|
||||
vm::op::Op,
|
||||
};
|
||||
use crate::{obj::prelude::*, vm::op::Op};
|
||||
use std::{collections::BTreeMap, rc::Rc};
|
||||
|
||||
pub struct Frame {
|
||||
@@ -26,7 +23,7 @@ impl Frame {
|
||||
pub fn push(&mut self, obj_ref: DynRef) {
|
||||
self.stack.push(obj_ref)
|
||||
}
|
||||
|
||||
|
||||
pub fn pop(&mut self) -> Option<DynRef> {
|
||||
self.stack.pop()
|
||||
}
|
||||
@@ -59,4 +56,3 @@ impl Frame {
|
||||
&self.ops
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ use crate::obj::{NameId, Sym};
|
||||
pub enum Op {
|
||||
/// Push a value from a name ID.
|
||||
Push(NameId),
|
||||
|
||||
|
||||
/// Pop the top stack value into a local name (if supplied)
|
||||
Pop(Option<NameId>),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user