Skip to content

Commit

Permalink
all: support ; statements, allowing for oneliners like `./v -e 'imp…
Browse files Browse the repository at this point in the history
…ort os; println( os.ls(os.args[1])!.sorted(a > b) )' vlib/math` (#19345)
  • Loading branch information
spytheman authored Sep 14, 2023
1 parent b746083 commit f3aec20
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 5 deletions.
7 changes: 7 additions & 0 deletions cmd/tools/vast/vast.v
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ fn (t Tree) stmt(node ast.Stmt) &Node {
ast.AssertStmt { return t.assert_stmt(node) }
ast.ExprStmt { return t.expr_stmt(node) }
ast.Block { return t.block(node) }
ast.SemicolonStmt { return t.semicolon_stmt(node) }
ast.SqlStmt { return t.sql_stmt(node) }
ast.AsmStmt { return t.asm_stmt(node) }
ast.NodeError { return t.node_error(node) }
Expand Down Expand Up @@ -1747,6 +1748,12 @@ fn (t Tree) sql_expr(node ast.SqlExpr) &Node {
return obj
}

fn (t Tree) semicolon_stmt(node ast.SemicolonStmt) &Node {
mut obj := new_object()
obj.add('pos', t.pos(node.pos))
return obj
}

fn (t Tree) sql_stmt(node ast.SqlStmt) &Node {
mut obj := new_object()
obj.add_terse('ast_type', t.string_node('SqlStmt'))
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub type Stmt = AsmStmt
| Module
| NodeError
| Return
| SemicolonStmt
| SqlStmt
| StructDecl
| TypeDecl
Expand Down Expand Up @@ -300,6 +301,11 @@ pub:
is_skipped bool // module main can be skipped in single file programs
}

pub struct SemicolonStmt {
pub:
pos token.Pos
}

[minify]
pub struct StructField {
pub:
Expand Down
1 change: 1 addition & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -2099,6 +2099,7 @@ fn (mut c Checker) stmt(mut node ast.Stmt) {
c.return_stmt(mut node)
c.scope_returns = true
}
ast.SemicolonStmt {}
ast.SqlStmt {
c.sql_stmt(mut node)
}
Expand Down
13 changes: 9 additions & 4 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ pub fn fmt(file ast.File, table &ast.Table, pref_ &pref.Preferences, is_debug bo
imp_str := f.out_imports.str().trim_space()
if imp_str.len > 0 {
return res + '\n' + imp_str + '\n'
} else {
return res
}
} else {
return res[..f.import_pos] + f.out_imports.str() + res[f.import_pos..]
return res
}
source_for_imports := res[..f.import_pos] + f.out_imports.str()
source_after_imports := res[f.import_pos..]
return source_for_imports + source_after_imports
}

pub fn (mut f Fmt) process_file_imports(file &ast.File) {
Expand Down Expand Up @@ -405,6 +405,9 @@ fn (f Fmt) should_insert_newline_before_node(node ast.Node, prev_node ast.Node)
return true
}
}
ast.SemicolonStmt {
return false
}
// Force a newline after struct declarations
ast.StructDecl {
return true
Expand Down Expand Up @@ -552,6 +555,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
ast.Return {
f.return_stmt(node)
}
ast.SemicolonStmt {}
ast.SqlStmt {
f.sql_stmt(node)
}
Expand All @@ -568,6 +572,7 @@ fn stmt_is_single_line(stmt ast.Stmt) bool {
return match stmt {
ast.ExprStmt, ast.AssertStmt { expr_is_single_line(stmt.expr) }
ast.Return, ast.AssignStmt, ast.BranchStmt { true }
ast.SemicolonStmt { true }
else { false }
}
}
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/fmt/tests/semicolons_expected.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os

println(os.args)
println('hello')
4 changes: 4 additions & 0 deletions vlib/v/fmt/tests/semicolons_input.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os;

println(os.args);
println('hello');
3 changes: 3 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,9 @@ fn (mut g Gen) stmt(node ast.Stmt) {
ast.Return {
g.return_stmt(node)
}
ast.SemicolonStmt {
g.writeln(';')
}
ast.SqlStmt {
g.sql_stmt(node)
}
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/gen/golang/golang.v
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,9 @@ pub fn (mut f Gen) stmt(node ast.Stmt) {
ast.Return {
f.return_stmt(node)
}
ast.SemicolonStmt {
f.writeln(';')
}
ast.SqlStmt {
f.sql_stmt(node)
}
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/gen/js/js.v
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,9 @@ fn (mut g JsGen) stmt_no_semi(node_ ast.Stmt) {
}
g.gen_return_stmt(node)
}
ast.SemicolonStmt {
g.writeln(';')
}
ast.SqlStmt {}
ast.StructDecl {
g.write_v_source_line_info(node.pos)
Expand Down Expand Up @@ -842,6 +845,9 @@ fn (mut g JsGen) stmt(node_ ast.Stmt) {
}
g.gen_return_stmt(node)
}
ast.SemicolonStmt {
g.writeln(';')
}
ast.SqlStmt {}
ast.StructDecl {
g.write_v_source_line_info(node.pos)
Expand Down
1 change: 1 addition & 0 deletions vlib/v/markused/walker.v
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ pub fn (mut w Walker) stmt(node_ ast.Stmt) {
ast.HashStmt {}
ast.Import {}
ast.InterfaceDecl {}
ast.SemicolonStmt {}
ast.Module {}
ast.TypeDecl {}
ast.NodeError {}
Expand Down
13 changes: 12 additions & 1 deletion vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -1122,13 +1122,24 @@ fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
.key_asm {
return p.asm_stmt(false)
}
.semicolon {
return p.semicolon_stmt()
}
// literals, 'if', etc. in here
else {
return p.parse_multi_expr(is_top_level)
}
}
}

fn (mut p Parser) semicolon_stmt() ast.SemicolonStmt {
pos := p.tok.pos()
p.check(.semicolon)
return ast.SemicolonStmt{
pos: pos
}
}

fn (mut p Parser) asm_stmt(is_top_level bool) ast.AsmStmt {
p.inside_asm = true
p.inside_asm_template = true
Expand Down Expand Up @@ -3647,7 +3658,7 @@ fn (mut p Parser) import_stmt() ast.Import {
}
pos_t := p.tok.pos()
if import_pos.line_nr == pos_t.line_nr {
if p.tok.kind !in [.lcbr, .eof, .comment] {
if p.tok.kind !in [.lcbr, .eof, .comment, .semicolon] {
p.error_with_pos('cannot import multiple modules at a time', pos_t)
return import_node
}
Expand Down
1 change: 1 addition & 0 deletions vlib/v/transformer/transformer.v
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ pub fn (mut t Transformer) stmt(mut node ast.Stmt) ast.Stmt {
expr = t.expr(mut expr)
}
}
ast.SemicolonStmt {}
ast.SqlStmt {}
ast.StructDecl {
for mut field in node.fields {
Expand Down

0 comments on commit f3aec20

Please sign in to comment.