Skip to content

Commit

Permalink
include local references in emitted ast
Browse files Browse the repository at this point in the history
  • Loading branch information
timbod7 committed Dec 8, 2022
1 parent 8135a06 commit 8c0a940
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 104 deletions.
46 changes: 39 additions & 7 deletions adl/stdlib/sys/adlast2.adl
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ struct ScopedName
Ident name;
};

union TypeRef
{
Ident primitive;
Ident typeParam;
ScopedName reference;
};

struct TypeExpr<R>
{
R typeRef;
Expand Down Expand Up @@ -112,4 +105,43 @@ struct Spanned<T> {
Span span;
};

union PrimitiveType {
Void Void;
Void Bool;
Void Int8;
Void Int16;
Void Int32;
Void Int64;
Void Word8;
Void Word16;
Void Word32;
Void Word64;
Void Float;
Void Double;
Void Json;
Void ByteVector;
Void String;
Void Vector;
Void StringMap;
Void Nullable;
Void TypeToken;
};

union TypeRef {
ScopedName scopedName;
Ident localName;
PrimitiveType primitive;
Ident typeParam;
};

// AST after initial parsing
type TypeExpr0 = TypeExpr<ScopedName>;
type Module0 = Module<TypeExpr0>;


// AST after name resolution
type TypeExpr1 = TypeExpr<TypeRef>;
type Decl1 = Decl<TypeExpr1>;
type Module1 = Module<TypeExpr1>;

};
78 changes: 66 additions & 12 deletions rust/compiler/src/adlgen/sys/adlast2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ impl ScopedName {
}
}

#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
pub enum TypeRef {
#[serde(rename="primitive")]
Primitive(Ident),

#[serde(rename="typeParam")]
TypeParam(Ident),

#[serde(rename="reference")]
Reference(ScopedName),
}

#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
pub struct TypeExpr<R> {
#[serde(rename="typeRef")]
Expand Down Expand Up @@ -279,3 +267,69 @@ impl<T> Spanned<T> {
}
}
}

#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
pub enum PrimitiveType {
Void,

Bool,

Int8,

Int16,

Int32,

Int64,

Word8,

Word16,

Word32,

Word64,

Float,

Double,

Json,

ByteVector,

String,

Vector,

StringMap,

Nullable,

TypeToken,
}

#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
pub enum TypeRef {
#[serde(rename="scopedName")]
ScopedName(ScopedName),

#[serde(rename="localName")]
LocalName(Ident),

#[serde(rename="primitive")]
Primitive(PrimitiveType),

#[serde(rename="typeParam")]
TypeParam(Ident),
}

pub type TypeExpr0 = TypeExpr<ScopedName>;

pub type Module0 = Module<TypeExpr0>;

pub type TypeExpr1 = TypeExpr<TypeRef>;

pub type Decl1 = Decl<TypeExpr1>;

pub type Module1 = Module<TypeExpr1>;
113 changes: 45 additions & 68 deletions rust/compiler/src/processing/primitives.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,50 @@
#[derive(serde::Serialize)]
pub enum PrimitiveType {
Void,
Bool,
Int8,
Int16,
Int32,
Int64,
Word8,
Word16,
Word32,
Word64,
Float,
Double,
Json,
ByteVector,
String,
Vector,
StringMap,
Nullable,
TypeToken,
}
use crate::adlgen::sys::adlast2::PrimitiveType;

impl PrimitiveType {
pub fn to_str(&self) -> &str {
match self {
PrimitiveType::Void => "Void",
PrimitiveType::Bool => "Bool",
PrimitiveType::Int8 => "Int8",
PrimitiveType::Int16 => "Int16",
PrimitiveType::Int32 => "Int32",
PrimitiveType::Int64 => "Int64",
PrimitiveType::Word8 => "Word8",
PrimitiveType::Word16 => "Word16",
PrimitiveType::Word32 => "Word32",
PrimitiveType::Word64 => "Word64",
PrimitiveType::Float => "Float",
PrimitiveType::Double => "Double",
PrimitiveType::Json => "Json",
PrimitiveType::ByteVector => "ByteVector",
PrimitiveType::String => "String",
PrimitiveType::Vector => "Vector",
PrimitiveType::StringMap => "StringMap",
PrimitiveType::Nullable => "Nullable",
PrimitiveType::TypeToken => "TypeToken",
}
pub fn str_from_prim(prim: PrimitiveType) -> &'static str {
match prim {
PrimitiveType::Void => "Void",
PrimitiveType::Bool => "Bool",
PrimitiveType::Int8 => "Int8",
PrimitiveType::Int16 => "Int16",
PrimitiveType::Int32 => "Int32",
PrimitiveType::Int64 => "Int64",
PrimitiveType::Word8 => "Word8",
PrimitiveType::Word16 => "Word16",
PrimitiveType::Word32 => "Word32",
PrimitiveType::Word64 => "Word64",
PrimitiveType::Float => "Float",
PrimitiveType::Double => "Double",
PrimitiveType::Json => "Json",
PrimitiveType::ByteVector => "ByteVector",
PrimitiveType::String => "String",
PrimitiveType::Vector => "Vector",
PrimitiveType::StringMap => "StringMap",
PrimitiveType::Nullable => "Nullable",
PrimitiveType::TypeToken => "TypeToken",
}
}

pub fn from_str(s: &str) -> Option<Self> {
match s {
"Void" => Some(Self::Void),
"Bool" => Some(Self::Bool),
"Int8" => Some(Self::Int8),
"Int16" => Some(Self::Int16),
"Int32" => Some(Self::Int32),
"Int64" => Some(Self::Int64),
"Word8" => Some(Self::Word8),
"Word16" => Some(Self::Word16),
"Word32" => Some(Self::Word32),
"Word64" => Some(Self::Word64),
"Float" => Some(Self::Float),
"Double" => Some(Self::Double),
"Json" => Some(Self::Json),
"ByteVector" => Some(Self::ByteVector),
"String" => Some(Self::String),
"Vector" => Some(Self::Vector),
"StringMap" => Some(Self::StringMap),
"Nullable" => Some(Self::Nullable),
"TypeToken" => Some(Self::TypeToken),
_ => None,
}
pub fn prim_from_str(s: &str) -> Option<PrimitiveType> {
match s {
"Void" => Some(PrimitiveType::Void),
"Bool" => Some(PrimitiveType::Bool),
"Int8" => Some(PrimitiveType::Int8),
"Int16" => Some(PrimitiveType::Int16),
"Int32" => Some(PrimitiveType::Int32),
"Int64" => Some(PrimitiveType::Int64),
"Word8" => Some(PrimitiveType::Word8),
"Word16" => Some(PrimitiveType::Word16),
"Word32" => Some(PrimitiveType::Word32),
"Word64" => Some(PrimitiveType::Word64),
"Float" => Some(PrimitiveType::Float),
"Double" => Some(PrimitiveType::Double),
"Json" => Some(PrimitiveType::Json),
"ByteVector" => Some(PrimitiveType::ByteVector),
"String" => Some(PrimitiveType::String),
"Vector" => Some(PrimitiveType::Vector),
"StringMap" => Some(PrimitiveType::StringMap),
"Nullable" => Some(PrimitiveType::Nullable),
"TypeToken" => Some(PrimitiveType::TypeToken),
_ => None,
}
}
24 changes: 7 additions & 17 deletions rust/compiler/src/processing/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use serde::Serialize;
use std::collections::{HashMap, HashSet};

use crate::adlgen::sys::adlast2 as adlast;
use crate::adlrt::custom::sys::types::map::Map;

use super::loader::AdlLoader;
use super::primitives::PrimitiveType;
use super::primitives::{prim_from_str, str_from_prim};
use super::{Module0, TypeExpr0};

type Result<T> = std::result::Result<T, ResolveError>;
Expand All @@ -20,18 +19,12 @@ pub enum ResolveError {
LoadFailed,
}

pub type TypeExpr1 = adlast::TypeExpr<TypeRef>;
pub type Decl1 = adlast::Decl<TypeExpr1>;
pub type Module1 = adlast::Module<TypeExpr1>;
pub type TypeRef = adlast::TypeRef;
pub type TypeExpr1 = adlast::TypeExpr1;
pub type Decl1 = adlast::Decl1;
pub type Module1 = adlast::Module1;
pub type ModuleName = adlast::ModuleName;

#[derive(Serialize)]
pub enum TypeRef {
ScopedName(adlast::ScopedName),
Primitive(PrimitiveType),
TypeParam(adlast::Ident),
}

pub struct Resolver {
loader: Box<dyn AdlLoader>,
modules: HashMap<ModuleName, Module1>,
Expand Down Expand Up @@ -299,14 +292,11 @@ impl<'a> ResolveCtx<'a> {
if self.type_params.contains(name.as_str()) {
return Ok(TypeRef::TypeParam(name.clone()));
}
if let Some(ptype) = PrimitiveType::from_str(&name) {
if let Some(ptype) = prim_from_str(&name) {
return Ok(TypeRef::Primitive(ptype));
}
if self.module0.decls.contains_key(name) {
return Ok(TypeRef::ScopedName(adlast::ScopedName::new(
self.module0.name.value.clone(),
name.clone(),
)));
return Ok(TypeRef::LocalName(name.clone()));
}
if let Some(scoped_name) = self.expanded_imports.get(name) {
return Ok(TypeRef::ScopedName(scoped_name.clone()));
Expand Down

0 comments on commit 8c0a940

Please sign in to comment.