Skip to content

Commit

Permalink
fix: fix test error
Browse files Browse the repository at this point in the history
  • Loading branch information
sbwtw committed Jul 20, 2024
1 parent ac28bb2 commit 2870965
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 97 deletions.
41 changes: 20 additions & 21 deletions lib/src/analysis/type_analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl TypeAnalyzer {

impl AstVisitorMut for TypeAnalyzer {
fn visit_literal_mut(&mut self, literal: &mut LiteralExpression) {
self.top_mut().derived_type = Some(Rc::new(literal.literal().ty()))
self.top_mut().derived_type = Some(literal.literal().ty())
}

fn visit_variable_expression_mut(&mut self, variable: &mut VariableExpression) {
Expand All @@ -87,11 +87,11 @@ impl AstVisitorMut for TypeAnalyzer {

let attr = self.top_mut();
let ty = match (derived_variable, derived_declaration) {
(Some(v), None) => v.ty().clone(),
(Some(v), None) => v.ty().cloned(),
(None, Some(decl)) => {
// update scope to inner declaration
attr.scope = decl_scope;
decl.read().unwrap().create_user_type().clone()
decl.read().unwrap().create_user_type()
}

// TODO: Ambiguity ?
Expand All @@ -111,24 +111,23 @@ impl AstVisitorMut for TypeAnalyzer {
operands_attr.push(self.pop());
}

let result_type = &mut self.top_mut().derived_type;
for attr in operands_attr {
if let Some(true) = attr
.derived_type
.as_deref()
.zip(result_type.as_deref())
.map(|(t1, t2)| t1.type_class() == t2.type_class())
{
continue;
}

if result_type.is_none() {
result_type.clone_from(&attr.derived_type);
continue;
}
}

expr.set_ty(result_type.map(|x| x.as_ref().clone()));
// let ref mut result_type = self.top_mut().derived_type;
// for attr in operands_attr {
// if let Some(true) = attr
// .derived_type
// .zip(result_type)
// .map(|(t1, t2)| t1.type_class() == t2.type_class())
// {
// continue;
// }
//
// if result_type.is_none() {
// result_type.clone_from(&attr.derived_type);
// continue;
// }
// }
//
// expr.set_ty(result_type.map(|x| x.clone()));
}

fn visit_assign_expression_mut(&mut self, assign: &mut AssignExpression) {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/ast/assign_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct AssignExpression {
left: Expression,
right: Expression,
assign_type: AssignType,
ty: Option<Box<dyn Type>>,
ty: Option<Type>,
}

impl_ast_display!(AssignExpression, visit_assign_expression);
Expand Down Expand Up @@ -59,11 +59,11 @@ impl AssignExpression {
&mut self.right
}

pub fn ty(&self) -> Option<&Box<dyn Type>> {
pub fn ty(&self) -> Option<&Type> {
self.ty.as_ref()
}

pub fn set_ty(&mut self, ty: Option<Box<dyn Type>>) {
pub fn set_ty(&mut self, ty: Option<Type>) {
self.ty = ty
}
}
7 changes: 3 additions & 4 deletions lib/src/ast/compo_access_expression.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::ast::*;
use crate::{impl_ast_display, impl_into_expression};
use std::rc::Rc;

#[derive(Debug)]
pub struct CompoAccessExpression {
left: Expression,
right: Expression,
ty: Option<Rc<Box<dyn Type>>>,
ty: Option<Type>,
}

impl_ast_display!(CompoAccessExpression, visit_compo_access_expression);
Expand Down Expand Up @@ -37,11 +36,11 @@ impl CompoAccessExpression {
&mut self.right
}

pub fn ty(&self) -> Option<Rc<Box<dyn Type>>> {
pub fn ty(&self) -> Option<Type> {
self.ty.clone()
}

pub fn set_ty(&mut self, ty: Option<Rc<Box<dyn Type>>>) {
pub fn set_ty(&mut self, ty: Option<Type>) {
self.ty = ty
}
}
6 changes: 3 additions & 3 deletions lib/src/ast/function_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::rc::Rc;
pub struct FunctionDeclare {
name: StString,
decl_class: DeclareClass,
return_type: Option<Box<dyn Type>>,
return_type: Option<Type>,
parameters: SmallVec8<Rc<Variable>>,
}

Expand All @@ -16,7 +16,7 @@ impl FunctionDeclare {
pub fn new(
name: StString,
class: DeclareClass,
ty: Option<Box<dyn Type>>,
ty: Option<Type>,
variables: SmallVec8<Rc<Variable>>,
) -> Self {
Self {
Expand All @@ -35,7 +35,7 @@ impl FunctionDeclare {
&self.decl_class
}

pub fn return_type(&self) -> &Option<Box<dyn Type>> {
pub fn return_type(&self) -> &Option<Type> {
&self.return_type
}

Expand Down
19 changes: 14 additions & 5 deletions lib/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,21 @@ impl_into_expression!(LiteralValue, |x| Expression::literal(Box::new(
LiteralExpression::new(x)
)));

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Type {
inner: Rc<TypeImpl>,
}

impl Type {
pub fn from_class(class: TypeClass) -> Self {
Self {
inner: TypeImpl {
class,
}
inner: Rc::new(TypeImpl { class }),
}
}

pub fn type_class(&self) -> &TypeClass {
&self.inner.class
}
}

impl Display for Type {
Expand All @@ -169,8 +171,15 @@ impl Display for Type {
}
}

#[derive(Debug)]
struct TypeImpl {
class: TypeClass
class: TypeClass,
}

impl Display for TypeImpl {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("{:?}", self.class))
}
}

bitflags! {
Expand Down
7 changes: 3 additions & 4 deletions lib/src/ast/operator_expression.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::ast::*;
use crate::{impl_ast_display, impl_into_expression};
use std::rc::Rc;

#[derive(Debug)]
pub struct OperatorExpression {
op: Operator,
ty: Option<Box<dyn Type>>,
ty: Option<Type>,
operands: SmallVec<[Expression; 2]>,
}

Expand Down Expand Up @@ -35,11 +34,11 @@ impl OperatorExpression {
&self.op
}

pub fn ty(&self) -> Option<&Box<dyn Type>> {
pub fn ty(&self) -> Option<&Type> {
self.ty.as_ref()
}

pub fn set_ty(&mut self, ty: Option<Box<dyn Type>>) {
pub fn set_ty(&mut self, ty: Option<Type>) {
self.ty = ty;
}

Expand Down
25 changes: 9 additions & 16 deletions lib/src/ast/types.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use crate::ast::*;
use crate::impl_has_attribute;
use smallmap::Map;
use std::convert::Into;
use std::rc::Rc;

use crate::ast::*;

macro_rules! builtin_type_impl {
(struct $name:ident, $class:expr) => {
#[derive(Debug, Clone, Default)]
pub struct $name;

impl $name {
pub fn new() -> Type {
pub fn new_type() -> Type {
Type::from_class($class)
}
}
Expand All @@ -36,7 +34,6 @@ pub struct UserType {
name: StString,
decl_id: Option<usize>,
class: Option<UserTypeClass>,
attributes: Map<StString, String>,
}

impl UserType {
Expand All @@ -45,7 +42,6 @@ impl UserType {
name,
decl_id: None,
class: None,
attributes: Map::new(),
}
}

Expand All @@ -54,7 +50,6 @@ impl UserType {
name,
decl_id: Some(proto),
class: None,
attributes: Map::new(),
}
}

Expand All @@ -63,15 +58,13 @@ impl UserType {
}
}

impl Into<Type> for UserType {}
impl From<UserType> for Type {
fn from(value: UserType) -> Self {
let class = TypeClass::UserType(value.name, None);

// impl Type for UserType {
// fn type_class(&self) -> TypeClass {
// TypeClass::UserType(self.name.clone(), self.class.clone())
// }
// }

impl_has_attribute!(UserType, attributes);
Type::from_class(class)
}
}

#[derive(Debug)]
pub struct EnumDeclare {
Expand Down
8 changes: 4 additions & 4 deletions lib/src/ast/variable_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use crate::parser::StString;
#[derive(Debug)]
pub struct VariableExpression {
name: StString,
ty: Option<Box<dyn Type>>,
ty: Option<Type>,
}

impl Clone for VariableExpression {
fn clone(&self) -> Self {
let ty = match &self.ty {
Some(t) => Some(t.as_ref().clone()),
Some(t) => Some(t.clone()),
_ => None,
};

Expand Down Expand Up @@ -44,12 +44,12 @@ impl VariableExpression {
}

#[inline]
pub fn ty(&self) -> Option<&Box<dyn Type>> {
pub fn ty(&self) -> Option<&Type> {
self.ty.as_ref()
}

#[inline]
pub fn set_ty(&mut self, ty: Option<Box<dyn Type>>) {
pub fn set_ty(&mut self, ty: Option<Type>) {
self.ty = ty
}
}
4 changes: 2 additions & 2 deletions lib/src/context/module_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ impl PrototypeImpl {
self.decl.variables()
}

pub fn create_user_type(&self) -> Option<Rc<Box<dyn Type>>> {
pub fn create_user_type(&self) -> Option<Type> {
// Only Structure types can be created as UserType
match self.decl.kind {
DeclKind::Struct(_) | DeclKind::Alias(_) | DeclKind::Enum(_) => {}
_ => return None,
}

let user_ty = UserType::from_proto(self.decl.identifier().clone(), self.id);
Some(Rc::new(Box::new(user_ty)))
Some(user_ty.into())
}

/// Get return value of prototype
Expand Down
6 changes: 3 additions & 3 deletions lib/src/parser/default_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ impl<I: Iterator<Item = LexerResult>> DefaultParserImpl<I> {
let token = self.next_token()?;

match &token.kind {
TokenKind::Int => Ok(Some(Box::new(IntType::new()))),
TokenKind::Bool => Ok(Some(Box::new(BoolType::new()))),
TokenKind::Identifier(ident) => Ok(Some(Box::new(UserType::from_name(ident.clone())))),
TokenKind::Int => Ok(Some(IntType::new_type())),
TokenKind::Bool => Ok(Some(BoolType::new_type())),
TokenKind::Identifier(ident) => Ok(Some(UserType::from_name(ident.clone()).into())),
_ => {
self.next = pos;
Ok(None)
Expand Down
14 changes: 8 additions & 6 deletions lib/src/parser/lalrpop_impl/st.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ extern {
"BOOL" => TokenKind::Bool,
"REAL" => TokenKind::Real,
"BYTE" => TokenKind::Byte,
"BIT" => TokenKind::Bit,
"LITERAL" => TokenKind::Literal(<LiteralValue>),
"IDENTIFIER" => TokenKind::Identifier(<StString>),
}
Expand Down Expand Up @@ -262,12 +263,13 @@ EnumFieldDecl: Rc<Variable> = {
}

/// Type
Type: Box<dyn Type> = {
"INT" => Box::new(IntType::new()),
"BOOL" => Box::new(BoolType::new()),
"BYTE" => Box::new(ByteType::new()),
"REAL" => Box::new(RealType::new()),
"IDENTIFIER" => Box::new(UserType::from_name(<>)),
Type: Type = {
"BIT" => BitType::new_type(),
"INT" => IntType::new_type(),
"BOOL" => BoolType::new_type(),
"BYTE" => ByteType::new_type(),
"REAL" => RealType::new_type(),
"IDENTIFIER" => UserType::from_name(<>).into(),
}

/// Variable declare groups flat
Expand Down
Loading

0 comments on commit 2870965

Please sign in to comment.