Skip to content

Commit

Permalink
refactor: simplify AST type definition and make type erasure pass mor…
Browse files Browse the repository at this point in the history
…e stable (#972)

Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Jan 4, 2024
1 parent a8e78c1 commit 508af9b
Show file tree
Hide file tree
Showing 104 changed files with 259 additions and 700 deletions.
27 changes: 13 additions & 14 deletions kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
//! :note: When the definition of any AST node is modified or the AST node
//! is added/deleted, it is necessary to modify the corresponding processing
//! in the compiler and regenerate the walker code.
//! :copyright: Copyright 2020 The KCL Authors. All rights reserved.
//!
//! todo: remove type_str fields after python frontend removed.
//! :copyright: Copyright The KCL Authors. All rights reserved.
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
Expand Down Expand Up @@ -435,8 +433,6 @@ pub struct UnificationStmt {
pub struct AssignStmt {
pub targets: Vec<NodeRef<Identifier>>,
pub value: NodeRef<Expr>,
pub type_annotation: Option<NodeRef<String>>,

pub ty: Option<NodeRef<Type>>,
}

Expand Down Expand Up @@ -608,11 +604,9 @@ impl SchemaStmt {
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct SchemaIndexSignature {
pub key_name: Option<String>,
pub key_type: NodeRef<String>,
pub value_type: NodeRef<String>,
pub value: Option<NodeRef<Expr>>,
pub any_other: bool,

pub key_ty: NodeRef<Type>,
pub value_ty: NodeRef<Type>,
}

Expand All @@ -626,7 +620,6 @@ pub struct SchemaIndexSignature {
pub struct SchemaAttr {
pub doc: String,
pub name: NodeRef<String>,
pub type_str: NodeRef<String>,
pub op: Option<BinOrAugOp>,
pub value: Option<NodeRef<Expr>>,
pub is_optional: bool,
Expand Down Expand Up @@ -996,9 +989,7 @@ pub struct CheckExpr {
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct LambdaExpr {
pub args: Option<NodeRef<Arguments>>,
pub return_type_str: Option<String>,
pub body: Vec<NodeRef<Stmt>>,

pub return_ty: Option<NodeRef<Type>>,
}

Expand Down Expand Up @@ -1039,9 +1030,6 @@ pub struct Keyword {
pub struct Arguments {
pub args: Vec<NodeRef<Identifier>>,
pub defaults: Vec<Option<NodeRef<Expr>>>,
pub type_annotation_list: Vec<Option<NodeRef<String>>>,

#[serde(default)]
pub ty_list: Vec<Option<NodeRef<Type>>>,
}

Expand Down Expand Up @@ -1684,6 +1672,17 @@ impl ToString for Type {
}
}

impl From<String> for Type {
/// Build a named type from the string.
fn from(value: String) -> Self {
Type::Named(Identifier {
names: vec![Node::dummy_node(value)],
pkgpath: "".to_string(),
ctx: ExprContext::Load,
})
}
}

impl From<token::BinOpToken> for AugOp {
fn from(op_kind: token::BinOpToken) -> Self {
match op_kind {
Expand Down
3 changes: 0 additions & 3 deletions kclvm/ast/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ fn build_assign_node(attr_name: &str, assign_value: NodeRef<Expr>) -> NodeRef<St
node_ref!(Stmt::Assign(AssignStmt {
value: assign_value,
targets: vec![iden],
type_annotation: None,
ty: None
}))
}
Expand Down Expand Up @@ -50,7 +49,6 @@ fn get_dummy_assign_ast() -> ast::Node<ast::AssignStmt> {
end_line,
end_column,
)),
type_annotation: None,
ty: None,
},
String::from(filename),
Expand Down Expand Up @@ -115,7 +113,6 @@ fn get_dummy_assign_binary_ast() -> ast::Node<ast::AssignStmt> {
end_line,
end_column,
)),
type_annotation: None,
ty: None,
},
String::from(filename),
Expand Down
24 changes: 13 additions & 11 deletions kclvm/ast_pretty/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use kclvm_ast::{
use super::{Indentation, Printer};

type ParameterType<'a> = (
(
&'a ast::NodeRef<ast::Identifier>,
&'a Option<ast::NodeRef<String>>,
),
(&'a ast::NodeRef<ast::Identifier>, Option<String>),
&'a Option<ast::NodeRef<ast::Expr>>,
);

Expand Down Expand Up @@ -224,11 +221,11 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
if let Some(key_name) = &index_signature.node.key_name {
self.write(&format!("{}: ", key_name));
}
self.write(&index_signature.node.key_type.node);
self.write(&index_signature.node.key_ty.node.to_string());
self.write_token(TokenKind::CloseDelim(DelimToken::Bracket));
self.write_token(TokenKind::Colon);
self.write_space();
self.write(&index_signature.node.value_type.node);
self.write(&index_signature.node.value_ty.node.to_string());
if let Some(value) = &index_signature.node.value {
self.write(" = ");
self.expr(value);
Expand Down Expand Up @@ -350,7 +347,7 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
self.write("?");
}
self.write(": ");
self.write(&schema_attr.type_str.node);
self.write(&schema_attr.ty.node.to_string());
if let Some(op) = &schema_attr.op {
let symbol = match op {
ast::BinOrAugOp::Bin(bin_op) => bin_op.symbol(),
Expand Down Expand Up @@ -671,11 +668,11 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
self.write_space();
self.walk_arguments(&args.node);
}
if let Some(ty_str) = &lambda_expr.return_type_str {
if let Some(ty_str) = &lambda_expr.return_ty {
self.write_space();
self.write_token(TokenKind::RArrow);
self.write_space();
self.write(ty_str);
self.write(&ty_str.node.to_string());
}
self.write_space();
self.write_token(TokenKind::OpenDelim(DelimToken::Brace));
Expand All @@ -702,7 +699,12 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
let parameter_zip_list: Vec<ParameterType<'_>> = arguments
.args
.iter()
.zip(arguments.type_annotation_list.iter())
.zip(
arguments
.ty_list
.iter()
.map(|ty| ty.clone().map(|n| n.node.to_string())),
)
.zip(arguments.defaults.iter())
.collect();
interleave!(
Expand All @@ -711,7 +713,7 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
let ((arg, ty_str), default) = para;
self.walk_identifier(&arg.node);
if let Some(ty_str) = ty_str {
self.write(&format!(": {}", ty_str.node));
self.write(&format!(": {}", ty_str));
}
if let Some(default) = default {
self.write(" = ");
Expand Down
15 changes: 9 additions & 6 deletions kclvm/compiler/src/codegen/llvm/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
let mut value = self
.walk_expr(&assign_stmt.value)
.expect(kcl_error::COMPILE_ERROR_MSG);
if let Some(type_annotation) = &assign_stmt.type_annotation {
let type_annotation = self.native_global_string_value(&type_annotation.node);
if let Some(ty) = &assign_stmt.ty {
let type_annotation = self.native_global_string_value(&ty.node.to_string());
let is_in_schema =
self.schema_stack.borrow().len() > 0 || self.schema_expr_stack.borrow().len() > 0;
value = self.build_call(
Expand Down Expand Up @@ -780,10 +780,13 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
schema_name_native_str,
index_sign_value,
key_name_str_ptr.into(),
self.native_global_string(index_signature.node.key_type.node.as_str(), "")
.into(),
self.native_global_string(
index_signature.node.value_type.node.as_str(),
index_signature.node.key_ty.node.to_string().as_str(),
"",
)
.into(),
self.native_global_string(
index_signature.node.value_ty.node.to_string().as_str(),
"",
)
.into(),
Expand Down Expand Up @@ -1461,7 +1464,7 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
.expect(kcl_error::INTERNAL_ERROR_MSG);
let string_ptr_value = self.native_global_string(name, "").into();
let type_str_ptr_value = self
.native_global_string(&schema_attr.type_str.node, "")
.native_global_string(&schema_attr.ty.node.to_string(), "")
.into();
self.build_void_call(
&ApiFunc::kclvm_config_attr_map.name(),
Expand Down
3 changes: 0 additions & 3 deletions kclvm/parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1912,7 +1912,6 @@ impl<'a> Parser<'a> {
self.bump_keyword(kw::Lambda);

let mut args = None;
let mut return_type_str = None;
let mut return_ty = None;

// schema_arguments
Expand All @@ -1928,7 +1927,6 @@ impl<'a> Parser<'a> {
if let TokenKind::RArrow = self.token.kind {
self.bump_token(TokenKind::RArrow);
let typ = self.parse_type_annotation();
return_type_str = Some(typ.node.to_string());
return_ty = Some(typ);
}

Expand Down Expand Up @@ -1974,7 +1972,6 @@ impl<'a> Parser<'a> {
Box::new(Node::node(
Expr::Lambda(LambdaExpr {
args,
return_type_str,
return_ty,
body: stmt_list,
}),
Expand Down
Loading

0 comments on commit 508af9b

Please sign in to comment.