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