Skip to content

Commit

Permalink
zz
Browse files Browse the repository at this point in the history
  • Loading branch information
Peefy committed Oct 9, 2023
1 parent 328a589 commit 1eb1eb6
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 52 deletions.
32 changes: 16 additions & 16 deletions internal/scripts/update-kclvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ kclvm_source_dir="$topdir/internal"
set -x

# Copy KCLVM.
cp "$topdir/internal/scripts/cli/kcl" $kclvm_install_dir/bin/
cp "$topdir/internal/scripts/cli/kclvm" $kclvm_install_dir/bin/
cp "$topdir/internal/scripts/cli/kcl-plugin" $kclvm_install_dir/bin/
cp "$topdir/internal/scripts/cli/kcl-doc" $kclvm_install_dir/bin/
cp "$topdir/internal/scripts/cli/kcl-test" $kclvm_install_dir/bin/
cp "$topdir/internal/scripts/cli/kcl-lint" $kclvm_install_dir/bin/
cp "$topdir/internal/scripts/cli/kcl-fmt" $kclvm_install_dir/bin/
cp "$topdir/internal/scripts/cli/kcl-vet" $kclvm_install_dir/bin/
chmod +x $kclvm_install_dir/bin/kcl
chmod +x $kclvm_install_dir/bin/kclvm
chmod +x $kclvm_install_dir/bin/kcl-plugin
chmod +x $kclvm_install_dir/bin/kcl-doc
chmod +x $kclvm_install_dir/bin/kcl-test
chmod +x $kclvm_install_dir/bin/kcl-lint
chmod +x $kclvm_install_dir/bin/kcl-fmt
chmod +x $kclvm_install_dir/bin/kcl-vet
# cp "$topdir/internal/scripts/cli/kcl" $kclvm_install_dir/bin/
# cp "$topdir/internal/scripts/cli/kclvm" $kclvm_install_dir/bin/
# cp "$topdir/internal/scripts/cli/kcl-plugin" $kclvm_install_dir/bin/
# cp "$topdir/internal/scripts/cli/kcl-doc" $kclvm_install_dir/bin/
# cp "$topdir/internal/scripts/cli/kcl-test" $kclvm_install_dir/bin/
# cp "$topdir/internal/scripts/cli/kcl-lint" $kclvm_install_dir/bin/
# cp "$topdir/internal/scripts/cli/kcl-fmt" $kclvm_install_dir/bin/
# cp "$topdir/internal/scripts/cli/kcl-vet" $kclvm_install_dir/bin/
# chmod +x $kclvm_install_dir/bin/kcl
# chmod +x $kclvm_install_dir/bin/kclvm
# chmod +x $kclvm_install_dir/bin/kcl-plugin
# chmod +x $kclvm_install_dir/bin/kcl-doc
# chmod +x $kclvm_install_dir/bin/kcl-test
# chmod +x $kclvm_install_dir/bin/kcl-lint
# chmod +x $kclvm_install_dir/bin/kcl-fmt
# chmod +x $kclvm_install_dir/bin/kcl-vet

set +x

Expand Down
31 changes: 6 additions & 25 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions kclvm/ast_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,22 @@ impl<'p> Printer<'p> {
}
}

/// Wether has comments on ast node.
pub(crate) fn has_comments_on_node<T>(&mut self, node: &ast::NodeRef<T>) -> bool {
if !self.cfg.write_comments {
return false;
}
let mut index = None;
for (i, comment) in self.comments.iter().enumerate() {
if comment.line <= node.line {
index = Some(i);
} else {
break;
}
}
index.is_some()
}

/// Print ast comments.
pub fn write_ast_comments<T>(&mut self, node: &ast::NodeRef<T>) {
if !self.cfg.write_comments {
Expand Down Expand Up @@ -258,3 +274,10 @@ pub fn print_ast_node(node: ASTNode) -> String {
printer.write_node(node);
printer.out
}

/// Print schema expression AST node to string.
pub fn print_schema_expr(schema_expr: &ast::SchemaExpr) -> String {
let mut printer = Printer::default();
printer.walk_schema_expr(schema_expr);
printer.out
}
51 changes: 41 additions & 10 deletions kclvm/ast_pretty/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::HashSet;

use kclvm_error::bug;
use kclvm_ast::{
ast::{self, CallExpr},
token::{DelimToken, TokenKind},
walker::MutSelfTypedResultWalker,
};
use kclvm_error::bug;

use super::{Indentation, Printer};

Expand Down Expand Up @@ -39,6 +39,10 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
for comment in &module.comments {
self.comments.push_back(comment.clone());
}
if !module.doc.is_empty() {
self.write(&module.doc);
self.write_newline();
}
self.stmts(&module.body);
}

Expand Down Expand Up @@ -442,7 +446,15 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
.iter()
.map(|e| e.line)
.collect::<HashSet<u64>>();
let mut in_one_line = line_set.len() <= 1;
// There are comments in the configuration block.
let has_comment = !list_expr.elts.is_empty()
&& list_expr
.elts
.iter()
.map(|e| self.has_comments_on_node(e))
.all(|r| r);
// When there are comments in the configuration block, print them as multiline configurations.
let mut in_one_line = line_set.len() <= 1 && !has_comment;
if let Some(elt) = list_expr.elts.first() {
if let ast::Expr::ListIfItem(_) = &elt.node {
in_one_line = false;
Expand Down Expand Up @@ -527,6 +539,7 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
}
self.write(dict_comp.entry.operation.symbol());
self.write_space();
self.expr(&dict_comp.entry.value);
for gen in &dict_comp.generators {
self.walk_comp_clause(&gen.node);
}
Expand Down Expand Up @@ -596,10 +609,24 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {

fn walk_config_expr(&mut self, config_expr: &'ctx ast::ConfigExpr) -> Self::Result {
let line_set: HashSet<u64> = config_expr.items.iter().map(|item| item.line).collect();
let mut in_one_line = line_set.len() <= 1;
if let Some(item) = config_expr.items.first() {
if let ast::Expr::ConfigIfEntry(_) = &item.node.value.node {
in_one_line = false;
// There are comments in the configuration block.
let has_comment = !config_expr.items.is_empty()
&& config_expr
.items
.iter()
.map(|item| self.has_comments_on_node(item))
.all(|r| r);
// When there are comments in the configuration block, print them as multiline configurations.
let mut in_one_line = line_set.len() <= 1 && !has_comment;
// When there are complex configuration blocks in the configuration block, print them as multiline configurations.
if config_expr.items.len() == 1 && in_one_line {
if let Some(item) = config_expr.items.first() {
if matches!(
&item.node.value.node,
ast::Expr::ConfigIfEntry(_) | ast::Expr::Config(_) | ast::Expr::Schema(_)
) {
in_one_line = false;
}
}
}
self.write_token(TokenKind::OpenDelim(DelimToken::Brace));
Expand Down Expand Up @@ -649,7 +676,7 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
}
self.write_space();
self.write_token(TokenKind::OpenDelim(DelimToken::Brace));
self.write_newline();
self.write_newline_without_fill();
self.write_indentation(Indentation::Indent);

// lambda body
Expand Down Expand Up @@ -708,10 +735,14 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
}

fn walk_number_lit(&mut self, number_lit: &'ctx ast::NumberLit) -> Self::Result {
match number_lit.value {
match &number_lit.value {
ast::NumberLitValue::Int(int_val) => self.write(&int_val.to_string()),
ast::NumberLitValue::Float(float_val) => self.write(&float_val.to_string()),
}
// Number suffix e.g., 1Gi
if let Some(binary_suffix) = &number_lit.binary_suffix {
self.write(&binary_suffix.value())
}
}

fn walk_string_lit(&mut self, string_lit: &'ctx ast::StringLit) -> Self::Result {
Expand Down Expand Up @@ -817,12 +848,12 @@ impl<'p> Printer<'p> {
let names = &identifier.names;

let re = fancy_regex::Regex::new(IDENTIFIER_REGEX).unwrap();
let need_right_brace = !names.iter().all(|n| re.is_match(n).unwrap_or(false));
let need_right_brace = !names.iter().all(|n| re.is_match(&n).unwrap_or(false));
let count = if need_right_brace {
self.write(
&names
.iter()
.map(|n| n.replace('\"', "\\\""))
.map(|n| format!("{n:?}"))
.collect::<Vec<String>>()
.join(": {"),
);
Expand Down
2 changes: 1 addition & 1 deletion kclvm/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["target-webassembly", "llvm12-0"] }
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["target-webassembly", "llvm7-0"] }
time = "0.1"
phf = { version = "0.9", features = ["macros"] }
ahash = "0.7.2"
Expand Down

0 comments on commit 1eb1eb6

Please sign in to comment.