cgen: match: do not use a temp var; minor fixes
parent
cf094c6265
commit
900ada1112
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
default_vpm_server_urls = ['https://vpm.best', 'https://vpm.vlang.io']
|
default_vpm_server_urls = ['https://vpm.vlang.io']
|
||||||
valid_vpm_commands = ['help', 'search', 'install', 'update', 'remove']
|
valid_vpm_commands = ['help', 'search', 'install', 'update', 'remove']
|
||||||
excluded_dirs = ['cache', 'vlib']
|
excluded_dirs = ['cache', 'vlib']
|
||||||
supported_vcs_systems = ['git', 'hg']
|
supported_vcs_systems = ['git', 'hg']
|
||||||
|
@ -344,7 +344,7 @@ fn get_all_modules() []string {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if r.status_code != 200 {
|
if r.status_code != 200 {
|
||||||
println('Failed to search vpm.best. Status code: $r.status_code')
|
println('Failed to search vpm.vlang.io. Status code: $r.status_code')
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
s := r.text
|
s := r.text
|
||||||
|
|
|
@ -6,12 +6,12 @@ Usage:
|
||||||
Examples:
|
Examples:
|
||||||
v hello.v Compile the file `hello.v` and output it as `hello` or `hello.exe`.
|
v hello.v Compile the file `hello.v` and output it as `hello` or `hello.exe`.
|
||||||
v run hello.v Same as above but also run the produced executable immediately after compilation.
|
v run hello.v Same as above but also run the produced executable immediately after compilation.
|
||||||
v -o h.c hello.v Translate `hello.v` to `h.c` . Do not compile further.
|
v -o h.c hello.v Translate `hello.v` to `h.c`. Do not compile further.
|
||||||
|
|
||||||
The commands are:
|
The commands are:
|
||||||
build Build V code in the provided path (default).
|
build Build V code in the provided path (default).
|
||||||
create Setup the file structure for a V project.
|
create Setup the file structure for a V project.
|
||||||
doc Generates the documentation for a V module (coming soon in 0.3).
|
doc Generate the documentation for a V module.
|
||||||
fmt Format the V code provided.
|
fmt Format the V code provided.
|
||||||
repl Run the REPL.
|
repl Run the REPL.
|
||||||
run Compile and run a V program.
|
run Compile and run a V program.
|
||||||
|
|
|
@ -1,33 +0,0 @@
|
||||||
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
|
||||||
// Use of this source code is governed by an MIT license
|
|
||||||
// that can be found in the LICENSE file.
|
|
||||||
module main
|
|
||||||
|
|
||||||
import (
|
|
||||||
os
|
|
||||||
v.pref
|
|
||||||
)
|
|
||||||
|
|
||||||
fn create_symlink() {
|
|
||||||
$if windows {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
vexe := pref.vexe_path()
|
|
||||||
mut link_path := '/usr/local/bin/v'
|
|
||||||
mut ret := os.exec('ln -sf $vexe $link_path') or { panic(err) }
|
|
||||||
if ret.exit_code == 0 {
|
|
||||||
println('Symlink "$link_path" has been created')
|
|
||||||
}
|
|
||||||
else if os.system('uname -o | grep -q \'[A/a]ndroid\'') == 0 {
|
|
||||||
println('Failed to create symlink "$link_path". Trying again with Termux path for Android.')
|
|
||||||
link_path = '/data/data/com.termux/files/usr/bin/v'
|
|
||||||
ret = os.exec('ln -sf $vexe $link_path') or { panic(err) }
|
|
||||||
if ret.exit_code == 0 {
|
|
||||||
println('Symlink "$link_path" has been created')
|
|
||||||
} else {
|
|
||||||
println('Failed to create symlink "$link_path". Try again with sudo.')
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
println('Failed to create symlink "$link_path". Try again with sudo.')
|
|
||||||
}
|
|
||||||
}
|
|
25
cmd/v/v.v
25
cmd/v/v.v
|
@ -11,6 +11,7 @@ import (
|
||||||
os
|
os
|
||||||
v.table
|
v.table
|
||||||
v.doc
|
v.doc
|
||||||
|
v.pref
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -149,3 +150,27 @@ fn disallow_unknown_flags(prefs flag.MainCmdPreferences) {
|
||||||
println('V Error: Unexpected flag found: $prefs.unknown_flag')
|
println('V Error: Unexpected flag found: $prefs.unknown_flag')
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_symlink() {
|
||||||
|
$if windows {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vexe := pref.vexe_path()
|
||||||
|
mut link_path := '/usr/local/bin/v'
|
||||||
|
mut ret := os.exec('ln -sf $vexe $link_path') or { panic(err) }
|
||||||
|
if ret.exit_code == 0 {
|
||||||
|
println('Symlink "$link_path" has been created')
|
||||||
|
}
|
||||||
|
else if os.system('uname -o | grep -q \'[A/a]ndroid\'') == 0 {
|
||||||
|
println('Failed to create symlink "$link_path". Trying again with Termux path for Android.')
|
||||||
|
link_path = '/data/data/com.termux/files/usr/bin/v'
|
||||||
|
ret = os.exec('ln -sf $vexe $link_path') or { panic(err) }
|
||||||
|
if ret.exit_code == 0 {
|
||||||
|
println('Symlink "$link_path" has been created')
|
||||||
|
} else {
|
||||||
|
println('Failed to create symlink "$link_path". Try again with sudo.')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println('Failed to create symlink "$link_path". Try again with sudo.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -10,15 +10,15 @@ import (
|
||||||
|
|
||||||
pub type TypeDecl = AliasTypeDecl | SumTypeDecl | FnTypeDecl
|
pub type TypeDecl = AliasTypeDecl | SumTypeDecl | FnTypeDecl
|
||||||
|
|
||||||
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
|
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
|
||||||
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
|
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
|
||||||
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
|
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
|
||||||
CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr |
|
CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr |
|
||||||
ConcatExpr | Type | AsCast
|
ConcatExpr | Type | AsCast
|
||||||
|
|
||||||
pub type Stmt = GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
|
pub type Stmt = GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
|
||||||
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
|
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
|
||||||
HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt |
|
HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt |
|
||||||
LineComment | MultiLineComment | AssertStmt | UnsafeStmt | GoStmt
|
LineComment | MultiLineComment | AssertStmt | UnsafeStmt | GoStmt
|
||||||
// pub type Type = StructType | ArrayType
|
// pub type Type = StructType | ArrayType
|
||||||
// pub struct StructType {
|
// pub struct StructType {
|
||||||
|
@ -351,6 +351,7 @@ pub:
|
||||||
branches []MatchBranch
|
branches []MatchBranch
|
||||||
pos token.Position
|
pos token.Position
|
||||||
mut:
|
mut:
|
||||||
|
is_expr bool // returns a value
|
||||||
expr_type table.Type // type of `x` in `match x {`
|
expr_type table.Type // type of `x` in `match x {`
|
||||||
is_sum_type bool
|
is_sum_type bool
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,7 +84,7 @@ pub fn (g &Gen) styp(t string) string {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
//
|
||||||
pub fn (g mut Gen) write_typedef_types() {
|
pub fn (g mut Gen) write_typedef_types() {
|
||||||
for typ in g.table.types {
|
for typ in g.table.types {
|
||||||
match typ.kind {
|
match typ.kind {
|
||||||
|
@ -1019,10 +1019,10 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
|
||||||
if type_sym.kind != .void {
|
if type_sym.kind != .void {
|
||||||
tmp = g.new_tmp_var()
|
tmp = g.new_tmp_var()
|
||||||
}
|
}
|
||||||
styp := g.typ(node.expr_type)
|
//styp := g.typ(node.expr_type)
|
||||||
g.write('$styp $tmp = ')
|
//g.write('$styp $tmp = ')
|
||||||
g.expr(node.cond)
|
//g.expr(node.cond)
|
||||||
g.writeln(';') // $it.blocks.len')
|
//g.writeln(';') // $it.blocks.len')
|
||||||
// mut sum_type_str = ''
|
// mut sum_type_str = ''
|
||||||
for j, branch in node.branches {
|
for j, branch in node.branches {
|
||||||
if j == node.branches.len - 1 {
|
if j == node.branches.len - 1 {
|
||||||
|
@ -1036,14 +1036,22 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
|
||||||
g.write('if (')
|
g.write('if (')
|
||||||
for i, expr in branch.exprs {
|
for i, expr in branch.exprs {
|
||||||
if node.is_sum_type {
|
if node.is_sum_type {
|
||||||
g.write('${tmp}.typ == ')
|
g.expr(node.cond )
|
||||||
|
g.write('.typ == ')
|
||||||
|
//g.write('${tmp}.typ == ')
|
||||||
// sum_type_str
|
// sum_type_str
|
||||||
}
|
}
|
||||||
else if type_sym.kind == .string {
|
else if type_sym.kind == .string {
|
||||||
g.write('string_eq($tmp, ')
|
g.write('string_eq(')
|
||||||
|
//
|
||||||
|
g.expr(node.cond)
|
||||||
|
g.write(', ')
|
||||||
|
//g.write('string_eq($tmp, ')
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
g.write('$tmp == ')
|
g.expr(node.cond)
|
||||||
|
g.write(' == ')
|
||||||
|
//g.write('$tmp == ')
|
||||||
}
|
}
|
||||||
g.expr(expr)
|
g.expr(expr)
|
||||||
if type_sym.kind == .string {
|
if type_sym.kind == .string {
|
||||||
|
|
|
@ -163,14 +163,11 @@ void println(string s) {
|
||||||
|
|
||||||
void matches() {
|
void matches() {
|
||||||
int a = 100;
|
int a = 100;
|
||||||
int tmp1 = a;
|
if (a == 10) {
|
||||||
if (tmp1 == 10) {
|
|
||||||
println(tos3("10"));
|
println(tos3("10"));
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (tmp1 == 20) {
|
else if (a == 20) {
|
||||||
int k = a + 1;
|
int k = a + 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,12 +39,11 @@ void println(string s) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_expr(Expr e) {
|
void handle_expr(Expr e) {
|
||||||
Expr tmp1 = e;
|
if (e.typ == _type_idx_IfExpr) {
|
||||||
if (tmp1.typ == _type_idx_IfExpr) {
|
|
||||||
IfExpr* it = (IfExpr*)tmp1.obj; // ST it
|
IfExpr* it = (IfExpr*)tmp1.obj; // ST it
|
||||||
println(tos3("if"));
|
println(tos3("if"));
|
||||||
}
|
}
|
||||||
else if (tmp1.typ == _type_idx_IntegerLiteral) {
|
else if (e.typ == _type_idx_IntegerLiteral) {
|
||||||
IntegerLiteral* it = (IntegerLiteral*)tmp1.obj; // ST it
|
IntegerLiteral* it = (IntegerLiteral*)tmp1.obj; // ST it
|
||||||
println(tos3("integer"));
|
println(tos3("integer"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,9 @@ import (
|
||||||
v.pref
|
v.pref
|
||||||
term
|
term
|
||||||
os
|
os
|
||||||
//runtime
|
// runtime
|
||||||
sync
|
// sync
|
||||||
//time
|
// time
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -51,7 +51,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
|
||||||
pref: &pref.Preferences{}
|
pref: &pref.Preferences{}
|
||||||
scope: scope
|
scope: scope
|
||||||
// scope: &ast.Scope{start_pos: 0, parent: 0}
|
// scope: &ast.Scope{start_pos: 0, parent: 0}
|
||||||
|
|
||||||
}
|
}
|
||||||
p.init_parse_fns()
|
p.init_parse_fns()
|
||||||
p.read_first_token()
|
p.read_first_token()
|
||||||
|
@ -75,7 +75,7 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
|
||||||
parent: 0
|
parent: 0
|
||||||
}
|
}
|
||||||
// comments_mode: comments_mode
|
// comments_mode: comments_mode
|
||||||
|
|
||||||
}
|
}
|
||||||
p.read_first_token()
|
p.read_first_token()
|
||||||
// p.scope = &ast.Scope{start_pos: p.tok.position(), parent: 0}
|
// p.scope = &ast.Scope{start_pos: p.tok.position(), parent: 0}
|
||||||
|
@ -113,6 +113,7 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
struct Queue {
|
struct Queue {
|
||||||
mut:
|
mut:
|
||||||
idx int
|
idx int
|
||||||
|
@ -137,6 +138,8 @@ fn (q mut Queue) run() {
|
||||||
q.parsed_ast_files << file
|
q.parsed_ast_files << file
|
||||||
q.mu.unlock()
|
q.mu.unlock()
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
pub fn parse_files(paths []string, table &table.Table) []ast.File {
|
pub fn parse_files(paths []string, table &table.Table) []ast.File {
|
||||||
/*
|
/*
|
||||||
|
@ -661,7 +664,7 @@ pub fn (p mut Parser) name_expr() ast.Expr {
|
||||||
p.expr_mod = ''
|
p.expr_mod = ''
|
||||||
return ast.EnumVal{
|
return ast.EnumVal{
|
||||||
enum_name: enum_name // lp.prepend_mod(enum_name)
|
enum_name: enum_name // lp.prepend_mod(enum_name)
|
||||||
|
|
||||||
val: val
|
val: val
|
||||||
pos: p.tok.position()
|
pos: p.tok.position()
|
||||||
mod: mod
|
mod: mod
|
||||||
|
@ -992,7 +995,7 @@ fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
|
||||||
left: left
|
left: left
|
||||||
right: right
|
right: right
|
||||||
// right_type: typ
|
// right_type: typ
|
||||||
|
|
||||||
op: op
|
op: op
|
||||||
pos: pos
|
pos: pos
|
||||||
}
|
}
|
||||||
|
@ -1106,7 +1109,7 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
||||||
p.scope.register_var(ast.Var{
|
p.scope.register_var(ast.Var{
|
||||||
name: var_name
|
name: var_name
|
||||||
// expr: cond
|
// expr: cond
|
||||||
|
|
||||||
})
|
})
|
||||||
stmts := p.parse_block()
|
stmts := p.parse_block()
|
||||||
// println('nr stmts=$stmts.len')
|
// println('nr stmts=$stmts.len')
|
||||||
|
@ -1201,11 +1204,11 @@ fn (p mut Parser) if_expr() ast.Expr {
|
||||||
stmts: stmts
|
stmts: stmts
|
||||||
else_stmts: else_stmts
|
else_stmts: else_stmts
|
||||||
// typ: typ
|
// typ: typ
|
||||||
|
|
||||||
pos: pos
|
pos: pos
|
||||||
has_else: has_else
|
has_else: has_else
|
||||||
// left: left
|
// left: left
|
||||||
|
|
||||||
}
|
}
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
@ -1380,7 +1383,7 @@ fn (p mut Parser) const_decl() ast.ConstDecl {
|
||||||
fields << ast.Field{
|
fields << ast.Field{
|
||||||
name: name
|
name: name
|
||||||
// typ: typ
|
// typ: typ
|
||||||
|
|
||||||
}
|
}
|
||||||
exprs << expr
|
exprs << expr
|
||||||
// TODO: once consts are fixed reg here & update in checker
|
// TODO: once consts are fixed reg here & update in checker
|
||||||
|
|
Loading…
Reference in New Issue