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

Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Jan 4, 2024
1 parent 722dea3 commit f298b98
Show file tree
Hide file tree
Showing 102 changed files with 232 additions and 680 deletions.
26 changes: 12 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,16 @@ impl ToString for Type {
}
}

impl From<String> for Type {
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
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
61 changes: 16 additions & 45 deletions kclvm/parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ impl<'a> Parser<'a> {
Stmt::SchemaAttr(SchemaAttr {
doc: "".to_string(),
name: node_ref!(target.get_name(), targets[0].pos()),
type_str: type_annotation.unwrap(),
ty: ty.unwrap(),
op: Some(BinOrAugOp::Aug(aug_op)),
value: Some(value),
Expand Down Expand Up @@ -306,12 +305,7 @@ impl<'a> Parser<'a> {
self.skip_newlines();

Some(node_ref!(
Stmt::Assign(AssignStmt {
targets,
value,
type_annotation,
ty,
}),
Stmt::Assign(AssignStmt { targets, value, ty }),
self.token_span_pos(token, stmt_end_token)
))
} else {
Expand All @@ -322,7 +316,6 @@ impl<'a> Parser<'a> {
Stmt::SchemaAttr(SchemaAttr {
doc: "".to_string(),
name: node_ref!(target.get_names().join("."), targets[0].pos()),
type_str: type_annotation.unwrap(),
ty: ty.unwrap(),
op: None,
value: None,
Expand Down Expand Up @@ -377,7 +370,6 @@ impl<'a> Parser<'a> {
Stmt::Assign(AssignStmt {
targets: targets.clone(),
value: miss_expr,
type_annotation,
ty,
}),
pos,
Expand Down Expand Up @@ -833,7 +825,6 @@ impl<'a> Parser<'a> {
let mut args = Arguments {
args: Vec::new(),
defaults: Vec::new(),
type_annotation_list: Vec::new(),
ty_list: Vec::new(),
};

Expand Down Expand Up @@ -869,14 +860,12 @@ impl<'a> Parser<'a> {

let name = node_ref!(name, self.token_span_pos(name_pos, name_end));

let (type_annotation, type_annotation_node) = if let TokenKind::Colon = self.token.kind
{
let type_annotation_node = if let TokenKind::Colon = self.token.kind {
self.bump_token(TokenKind::Colon);
let typ = self.parse_type_annotation();

(Some(node_ref!(typ.node.to_string(), typ.pos())), Some(typ))
Some(typ)
} else {
(None, None)
None
};

let default_value = if let TokenKind::Assign = self.token.kind {
Expand All @@ -887,7 +876,6 @@ impl<'a> Parser<'a> {
};

args.args.push(name);
args.type_annotation_list.push(type_annotation);
args.ty_list.push(type_annotation_node);
args.defaults.push(default_value);
// Parameter interval comma
Expand Down Expand Up @@ -1047,17 +1035,16 @@ impl<'a> Parser<'a> {
if let Stmt::Assign(assign) = x.node.clone() {
if assign.targets.len() == 1 {
let ident = assign.targets[0].clone().node;
if let Some(type_str) = assign.type_annotation {
if !type_str.node.is_empty() {
if let Some(ref type_str) = assign.ty {
if !type_str.node.to_string().is_empty() {
body_body.push(node_ref!(
Stmt::SchemaAttr(SchemaAttr {
doc: "".to_string(),
name: node_ref!(
ident.get_names().join("."),
assign.targets[0].pos()
),
type_str,
ty: assign.ty.unwrap(),
ty: assign.ty.clone().unwrap(),
op: Some(BinOrAugOp::Aug(AugOp::Assign)),
value: Some(assign.value),
is_optional: false,
Expand Down Expand Up @@ -1212,8 +1199,7 @@ impl<'a> Parser<'a> {
self.bump_token(TokenKind::Colon);

// Parse the schema attribute type annotation.
let typ = self.parse_type_annotation();
let type_str = node_ref!(typ.node.to_string(), typ.pos());
let ty = self.parse_type_annotation();

let op = if self.token.kind == TokenKind::Assign {
self.bump_token(TokenKind::Assign);
Expand All @@ -1234,8 +1220,7 @@ impl<'a> Parser<'a> {
SchemaAttr {
doc,
name,
type_str,
ty: typ,
ty,
op,
value,
is_optional,
Expand Down Expand Up @@ -1284,14 +1269,10 @@ impl<'a> Parser<'a> {
/// LEFT_BRACKETS [NAME COLON] [ELLIPSIS] basic_type RIGHT_BRACKETS
/// COLON type [ASSIGN test] NEWLINE
fn parse_schema_index_signature(&mut self) -> SchemaIndexSignature {
let (key_name, key_type, any_other) = if matches!(self.token.kind, TokenKind::DotDotDot) {
let (key_name, key_ty, any_other) = if matches!(self.token.kind, TokenKind::DotDotDot) {
// bump token `...`
self.bump();
let key_type = {
let typ = self.parse_type_annotation();
node_ref!(typ.node.to_string(), typ.pos())
};
(None, key_type, true)
(None, self.parse_type_annotation(), true)
} else {
let token = self.token;
let expr = self.parse_identifier_expr();
Expand All @@ -1300,11 +1281,7 @@ impl<'a> Parser<'a> {
let key_name = ident.get_names().join(".");

if let TokenKind::CloseDelim(DelimToken::Bracket) = self.token.kind {
let key_type = {
let typ = node_ref!(Type::Named(ident), pos);
node_ref!(typ.node.to_string(), typ.pos())
};
(None, key_type, false)
(None, node_ref!(Type::Named(ident), pos), false)
} else {
self.bump_token(TokenKind::Colon);
let any_other = if let TokenKind::DotDotDot = self.token.kind {
Expand All @@ -1313,19 +1290,14 @@ impl<'a> Parser<'a> {
} else {
false
};
let key_type = {
let typ = self.parse_type_annotation();
node_ref!(typ.node.to_string(), typ.pos())
};
(Some(key_name), key_type, any_other)
(Some(key_name), self.parse_type_annotation(), any_other)
}
};

self.bump_token(TokenKind::CloseDelim(DelimToken::Bracket));
self.bump_token(TokenKind::Colon);

let typ = self.parse_type_annotation();
let value_type = node_ref!(typ.node.to_string(), typ.pos());
let value_ty = self.parse_type_annotation();

let value = if let TokenKind::Assign = self.token.kind {
self.bump();
Expand All @@ -1338,9 +1310,8 @@ impl<'a> Parser<'a> {

SchemaIndexSignature {
key_name,
key_type,
value_type,
value_ty: typ,
key_ty,
value_ty,
value,
any_other,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: parser/src/tests/ast.rs
assertion_line: 36
expression: "crate::tests::parsing_file_ast_json(\"hello.k\", r####\"a=123\"####)"
---
{"filename":"hello.k","pkg":"__main__","doc":null,"name":"__main__","body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"a","filename":"hello.k","line":1,"column":0,"end_line":1,"end_column":1}],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":1,"column":0,"end_line":1,"end_column":1}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":123}}},"filename":"hello.k","line":1,"column":2,"end_line":1,"end_column":5},"type_annotation":null,"ty":null}},"filename":"hello.k","line":1,"column":0,"end_line":1,"end_column":5}],"comments":[]}
{"filename":"hello.k","pkg":"__main__","doc":null,"name":"__main__","body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"a","filename":"hello.k","line":1,"column":0,"end_line":1,"end_column":1}],"pkgpath":"","ctx":"Store"},"filename":"hello.k","line":1,"column":0,"end_line":1,"end_column":1}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":123}}},"filename":"hello.k","line":1,"column":2,"end_line":1,"end_column":5},"ty":null}},"filename":"hello.k","line":1,"column":0,"end_line":1,"end_column":5}],"comments":[]}
Loading

0 comments on commit f298b98

Please sign in to comment.