-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add literal type default value pass and preprocess (#1583)
Signed-off-by: peefy <[email protected]>
- Loading branch information
Showing
10 changed files
with
207 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use kclvm_ast::ast; | ||
use kclvm_ast::walker::MutSelfMutWalker; | ||
|
||
#[derive(Default)] | ||
struct LitTypeDefaultValueTransformer; | ||
|
||
impl<'ctx> MutSelfMutWalker<'ctx> for LitTypeDefaultValueTransformer { | ||
fn walk_schema_attr(&mut self, schema_attr: &'ctx mut ast::SchemaAttr) { | ||
if schema_attr.value.is_none() && !schema_attr.is_optional { | ||
if let ast::Type::Literal(literal_ty) = &schema_attr.ty.node { | ||
let filename = schema_attr.ty.filename.clone(); | ||
let line = schema_attr.ty.end_line; | ||
// Append ` = ` width for th column. | ||
let column = schema_attr.ty.end_column + 3; | ||
schema_attr.op = Some(ast::AugOp::Assign); | ||
match literal_ty { | ||
ast::LiteralType::Bool(val) => { | ||
let column_offset = if *val { 4 } else { 5 }; | ||
schema_attr.value = Some(Box::new(ast::Node::new( | ||
ast::Expr::NameConstantLit(ast::NameConstantLit { | ||
value: if *val { | ||
ast::NameConstant::True | ||
} else { | ||
ast::NameConstant::False | ||
}, | ||
}), | ||
filename, | ||
line, | ||
column, | ||
line, | ||
column + column_offset, | ||
))); | ||
} | ||
ast::LiteralType::Int(val) => { | ||
let value = val.value.to_string(); | ||
let mut column_offset = value.len() as u64; | ||
if let Some(suffix) = &val.suffix { | ||
column_offset += suffix.value().len() as u64 | ||
} | ||
schema_attr.value = Some(Box::new(ast::Node::new( | ||
ast::Expr::NumberLit(ast::NumberLit { | ||
binary_suffix: val.suffix.clone(), | ||
value: ast::NumberLitValue::Int(val.value), | ||
}), | ||
filename, | ||
line, | ||
column, | ||
line, | ||
column + column_offset, | ||
))); | ||
} | ||
ast::LiteralType::Float(val) => { | ||
let value = kclvm_runtime::float_to_string(*val); | ||
let column_offset = value.len() as u64; | ||
schema_attr.value = Some(Box::new(ast::Node::new( | ||
ast::Expr::NumberLit(ast::NumberLit { | ||
binary_suffix: None, | ||
value: ast::NumberLitValue::Float(*val), | ||
}), | ||
filename, | ||
line, | ||
column, | ||
line, | ||
column + column_offset, | ||
))); | ||
} | ||
ast::LiteralType::Str(val) => { | ||
let value: ast::StringLit = val.to_string().into(); | ||
let column_offset = value.raw_value.len() as u64; | ||
schema_attr.value = Some(Box::new(ast::Node::new( | ||
ast::Expr::StringLit(value), | ||
filename, | ||
line, | ||
column, | ||
line, | ||
column + column_offset, | ||
))); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Fix literal type default value. e.g., `a: "value"` -> `a: "value" = "value"`. | ||
#[inline] | ||
pub fn fix_lit_ty_default_value(module: &'_ mut ast::Module) { | ||
LitTypeDefaultValueTransformer::default().walk_module(module); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
schema LitTyConfig: | ||
val0: "val" | ||
val1: 1 | ||
val2: 1Ki | ||
val3: 2.0 | ||
val4: True | ||
val5: False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
schema Config: | ||
val0: "val" | ||
val1: 1 | ||
val2: 1Ki | ||
val3: 2.0 | ||
val4: True | ||
val5: False | ||
|
||
c = Config {} |
7 changes: 7 additions & 0 deletions
7
test/grammar/schema/default_value/default_value_3/stdout.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
c: | ||
val0: val | ||
val1: 1 | ||
val2: 1024.0 | ||
val3: 2.0 | ||
val4: true | ||
val5: false |