cgen: match: do not use a temp var; minor fixes

pull/4038/head
Alexander Medvednikov 2020-03-16 10:53:28 +01:00
parent cf094c6265
commit 900ada1112
9 changed files with 71 additions and 71 deletions

View File

@ -9,7 +9,7 @@ import (
)
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']
excluded_dirs = ['cache', 'vlib']
supported_vcs_systems = ['git', 'hg']
@ -344,7 +344,7 @@ fn get_all_modules() []string {
panic(err)
}
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)
}
s := r.text

View File

@ -6,12 +6,12 @@ Usage:
Examples:
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 -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:
build Build V code in the provided path (default).
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.
repl Run the REPL.
run Compile and run a V program.

View File

@ -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.')
}
}

View File

@ -11,6 +11,7 @@ import (
os
v.table
v.doc
v.pref
)
const (
@ -149,3 +150,27 @@ fn disallow_unknown_flags(prefs flag.MainCmdPreferences) {
println('V Error: Unexpected flag found: $prefs.unknown_flag')
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.')
}
}

View File

@ -10,15 +10,15 @@ import (
pub type TypeDecl = AliasTypeDecl | SumTypeDecl | FnTypeDecl
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr |
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr |
ConcatExpr | Type | AsCast
pub type Stmt = GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt |
pub type Stmt = GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt |
LineComment | MultiLineComment | AssertStmt | UnsafeStmt | GoStmt
// pub type Type = StructType | ArrayType
// pub struct StructType {
@ -351,6 +351,7 @@ pub:
branches []MatchBranch
pos token.Position
mut:
is_expr bool // returns a value
expr_type table.Type // type of `x` in `match x {`
is_sum_type bool
}

View File

@ -84,7 +84,7 @@ pub fn (g &Gen) styp(t string) string {
}
*/
//
pub fn (g mut Gen) write_typedef_types() {
for typ in g.table.types {
match typ.kind {
@ -1019,10 +1019,10 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
if type_sym.kind != .void {
tmp = g.new_tmp_var()
}
styp := g.typ(node.expr_type)
g.write('$styp $tmp = ')
g.expr(node.cond)
g.writeln(';') // $it.blocks.len')
//styp := g.typ(node.expr_type)
//g.write('$styp $tmp = ')
//g.expr(node.cond)
//g.writeln(';') // $it.blocks.len')
// mut sum_type_str = ''
for j, branch in node.branches {
if j == node.branches.len - 1 {
@ -1036,14 +1036,22 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
g.write('if (')
for i, expr in branch.exprs {
if node.is_sum_type {
g.write('${tmp}.typ == ')
g.expr(node.cond )
g.write('.typ == ')
//g.write('${tmp}.typ == ')
// sum_type_str
}
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 {
g.write('$tmp == ')
g.expr(node.cond)
g.write(' == ')
//g.write('$tmp == ')
}
g.expr(expr)
if type_sym.kind == .string {

View File

@ -163,14 +163,11 @@ void println(string s) {
void matches() {
int a = 100;
int tmp1 = a;
if (tmp1 == 10) {
if (a == 10) {
println(tos3("10"));
}
else if (tmp1 == 20) {
else if (a == 20) {
int k = a + 1;
}
else {
}

View File

@ -39,12 +39,11 @@ void println(string s) {
}
void handle_expr(Expr e) {
Expr tmp1 = e;
if (tmp1.typ == _type_idx_IfExpr) {
if (e.typ == _type_idx_IfExpr) {
IfExpr* it = (IfExpr*)tmp1.obj; // ST it
println(tos3("if"));
}
else if (tmp1.typ == _type_idx_IntegerLiteral) {
else if (e.typ == _type_idx_IntegerLiteral) {
IntegerLiteral* it = (IntegerLiteral*)tmp1.obj; // ST it
println(tos3("integer"));
}

View File

@ -11,9 +11,9 @@ import (
v.pref
term
os
//runtime
sync
//time
// runtime
// sync
// time
)
const (
@ -51,7 +51,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
pref: &pref.Preferences{}
scope: scope
// scope: &ast.Scope{start_pos: 0, parent: 0}
}
p.init_parse_fns()
p.read_first_token()
@ -75,7 +75,7 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
parent: 0
}
// comments_mode: comments_mode
}
p.read_first_token()
// 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 {
mut:
idx int
@ -137,6 +138,8 @@ fn (q mut Queue) run() {
q.parsed_ast_files << file
q.mu.unlock()
}
*/
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 = ''
return ast.EnumVal{
enum_name: enum_name // lp.prepend_mod(enum_name)
val: val
pos: p.tok.position()
mod: mod
@ -992,7 +995,7 @@ fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
left: left
right: right
// right_type: typ
op: op
pos: pos
}
@ -1106,7 +1109,7 @@ fn (p mut Parser) for_statement() ast.Stmt {
p.scope.register_var(ast.Var{
name: var_name
// expr: cond
})
stmts := p.parse_block()
// println('nr stmts=$stmts.len')
@ -1201,11 +1204,11 @@ fn (p mut Parser) if_expr() ast.Expr {
stmts: stmts
else_stmts: else_stmts
// typ: typ
pos: pos
has_else: has_else
// left: left
}
return node
}
@ -1380,7 +1383,7 @@ fn (p mut Parser) const_decl() ast.ConstDecl {
fields << ast.Field{
name: name
// typ: typ
}
exprs << expr
// TODO: once consts are fixed reg here & update in checker