Skip to content

Commit

Permalink
feat(ir,lexer): fix build errors, expand ir types
Browse files Browse the repository at this point in the history
  • Loading branch information
LizAinslie committed Dec 23, 2023
1 parent 3ccb55c commit 0b2af02
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 55 deletions.
4 changes: 0 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
[package]
name = "maple"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html


[workspace]
members = ["crates/*"]
7 changes: 7 additions & 0 deletions crates/maple-ir/src/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::expr::Expr;

pub struct Block {
pub code: Vec<Box<dyn Expr>>,
}

impl Expr for Block {}
10 changes: 10 additions & 0 deletions crates/maple-ir/src/closure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::iter::Map;
use crate::block::Block;
use crate::types::Type;
use crate::values::Value;

pub struct Closure<'a> {
pub name: &'a str,
pub closed_values: Map<String, Box<dyn Value<dyn Type>>>,
pub body: Block,
}
1 change: 1 addition & 0 deletions crates/maple-ir/src/expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub trait Expr {}
26 changes: 8 additions & 18 deletions crates/maple-ir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
mod types;
mod values;
mod prototype;

pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
pub mod types;
pub mod values;
pub mod prototype;
pub mod closure;
pub mod block;
pub mod variable;
pub mod expr;
pub mod tuple;
38 changes: 36 additions & 2 deletions crates/maple-ir/src/prototype.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
use std::collections::HashMap;
use crate::closure::Closure;
use crate::types::Type;
use crate::values::Value;

pub struct Prototype <T : Type, V : Value<T>> {
value: V,
pub struct PrototypeMethod<'a> {
closure: &'a mut Closure<'a>,
}

impl<'a> PrototypeMethod<'a> {
pub fn new(closure: &'a mut Closure<'a>) -> Self {
Self {
closure,
}
}

pub fn get_name(&self) -> String {
self.closure.name.to_owned()
}
}

pub struct Prototype<'a, T : Type, V : Value<T>> {
pub(crate) value: V,
pub(crate) methods: HashMap<String, &'a mut PrototypeMethod<'a>>,
pub(crate) associated_type: T,
}

impl<'a, T : Type, V : Value<T>> Prototype<'a, T, V> {
pub fn add_method(mut self, method: &'a mut PrototypeMethod<'a>) -> Result<(), String> {
let method_name = method.get_name().to_owned();
if self.methods.contains_key(&method_name) {
return Err(format!("Method with name '{}' already exists for type {}", &method_name, self.associated_type.name()));
}
self.methods.insert(method_name, method);
Ok(())
}
}

pub trait PrototypeDefinition<T : Type, V : Value<T>> {
fn build_proto<'a>(self, _type: T) -> Prototype<'a, T, V>;
}
77 changes: 77 additions & 0 deletions crates/maple-ir/src/tuple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::collections::HashMap;
use std::marker::PhantomData;
use std::ops::Index;
use crate::expr::Expr;
use crate::prototype::{Prototype, PrototypeDefinition};
use crate::types::{Type, TypeInfo};
use crate::values::Value;

type TupleInnerValue = Box<dyn Value<Box<dyn Type>>>;

pub struct TupleValue {
pub size: i8,
pub values: Vec<TupleInnerValue>,
pub _type: TupleDefinition,
}

impl TypeInfo<TupleDefinition> for TupleValue {
fn get_type(self) -> TupleDefinition {
self._type
}
}

impl Value<TupleDefinition> for TupleValue {}

impl Index<usize> for TupleValue {
type Output = TupleInnerValue;

fn index(&self, index: usize) -> &Self::Output {
&self.values[index]
}
}

pub struct TupleDefinition {
pub type_name: String,
pub size: i8,
pub shape: Vec<Box<dyn Type>>,
}

impl Expr for TupleDefinition {}

impl Type for TupleDefinition {
fn name(self) -> String {
self.type_name
}
}

///
///
/// # Type Arguments
/// `T` The Tuple type this prototype definition is associated with
pub struct TuplePrototypeDefinition<T : Type, V> where V : Value<T> {
recipient: V,
type_name: String,
__marker: PhantomData<T>,
}

impl<T : Type, V : Value<T>> Expr for TuplePrototypeDefinition<T, V> {}

impl<T : Type, V : Value<T>> PrototypeDefinition<T, V> for TuplePrototypeDefinition<T, V> {
fn build_proto<'a>(self, _type: T) -> Prototype<'a, T, V> {
Prototype {
value: self.recipient,
associated_type: _type,
methods: HashMap::new(),
}
}
}

impl<T: Type, V: Value<T>> TuplePrototypeDefinition<T, V> {
pub fn new(recipient: V, _type: T) -> Self {
Self {
recipient,
type_name: _type.name(),
__marker: PhantomData::default(),
}
}
}
25 changes: 16 additions & 9 deletions crates/maple-ir/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
pub trait Type {}
use crate::expr::Expr;

pub trait Type : Expr {
fn name(self) -> String;
}

pub trait TypeInfo<T : Type> {
fn get_type(self) -> T;
Expand All @@ -12,13 +16,16 @@ pub enum PrimitiveType {
Nil,
}

impl Type for PrimitiveType {}
impl Expr for PrimitiveType {}

pub struct Tuple {
type_name: String,
size: i8,
shape: Vec<Box<dyn Type>>,
impl Type for PrimitiveType {
fn name(self) -> String {
match self {
PrimitiveType::String => "String".to_owned(),
PrimitiveType::Integer => "Int".to_owned(),
PrimitiveType::Float => "Float".to_owned(),
PrimitiveType::Boolean => "Boolean".to_owned(),
PrimitiveType::Nil => "Nil".to_owned()
}
}
}

impl Type for Tuple {}

2 changes: 1 addition & 1 deletion crates/maple-ir/src/values.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::types::{PrimitiveType, Type, TypeInfo};

pub trait Value<T : Type> {}
pub trait Value<T : Type> : TypeInfo<T> {}

pub enum PrimitiveValue {
StringValue(String),
Expand Down
15 changes: 15 additions & 0 deletions crates/maple-ir/src/variable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::expr::Expr;
use crate::types::Type;
use crate::values::Value;

pub enum VariableMetaType {
Static,
}

pub struct VariableDeclaration<T : Type, V : Value<T>> {
pub meta_type: VariableMetaType,
pub _type: T,
pub value: V,
}

impl<T : Type, V : Value<T>> Expr for VariableDeclaration<T, V> {}
8 changes: 4 additions & 4 deletions crates/maple-lexer/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ error_chain!{
}

foreign_links {
Io(std::io::Error) #[doc = "Wrapper around a `std::io::Error`"];
Utf8(std::str::Utf8Error) #[doc = "An error parsing data as UTF-8"];
FloatParsing(std::num::ParseFloatError) #[doc = "A float parsing error"];
IntParsing(std::num::ParseIntError) #[doc = "An integer parsing error"];
Io(::std::io::Error) #[doc = "Wrapper around a `std::io::Error`"];
Utf8(::std::str::Utf8Error) #[doc = "An error parsing data as UTF-8"];
FloatParsing(::std::num::ParseFloatError) #[doc = "A float parsing error"];
IntParsing(::std::num::ParseIntError) #[doc = "An integer parsing error"];

}
}
7 changes: 3 additions & 4 deletions crates/maple-lexer/src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::str;
use error_chain::bail;
use crate::codemap::Span;
use crate::errors::*;
use error_chain::example_generated::ResultExt;
use serde::{Deserialize, Serialize};


#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum TokenKind {
// Values
Integer(usize),
Expand Down Expand Up @@ -143,7 +143,6 @@ fn tokenize_number(data: &str) -> Result<(TokenKind, usize)> {
} else {
let n: usize = decimal.parse()?;
Ok((TokenKind::Integer(n), bytes_read))

}
}

Expand Down Expand Up @@ -221,7 +220,7 @@ pub fn tokenize_single_token(data: &str) -> Result<(TokenKind, usize)> {
'>' => (TokenKind::CloseAngle, 1),
'!' => (TokenKind::Exclamation, 1),
'?' => (TokenKind::Question, 1),
'0' ... '9' => tokenize_number(data).chain_err(|| "Couldn't tokenize a number")?,
'0' ..= '9' => tokenize_number(data).chain_err(|| "Couldn't tokenize a number")?,
c @ '_' | c if c.is_alphabetic() => tokenize_identifier(data)
.chain_err(|| "Couldn't tokenize an identifier")?,
other => bail!(ErrorKind::UnknownCharacter(other)),
Expand Down
10 changes: 5 additions & 5 deletions examples/maple-code/prototypes/proto_2023-12-22.maple
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ enum HairColor [

// Objects are a lot like classes in Typescript
object Person {
public val(prop) name: Name
public val(prop) age: Int
public val(prop) hairColor: HairColor
public val name: Name
public val age: Int
public val hairColor: HairColor

// properties can have default values - you a broke bitch
private val(prop) money: Int = 0
private val money: Int = 0

// The `constructor` tells the compiler that this function should statically
// create a new Person.
Expand Down Expand Up @@ -116,7 +116,7 @@ object Person {

// Objects can be defined and used out of order
object Paycheck {
public val(prop) amount: Double
public val amount: Double

// A static value that represents a tax rate of 5%
public val(static) TAX_RATE: Double = .05
Expand Down

0 comments on commit 0b2af02

Please sign in to comment.