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 ( 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

View File

@ -11,7 +11,7 @@ Examples:
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.

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

View File

@ -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
} }

View File

@ -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 {

View File

@ -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 {
} }

View File

@ -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"));
} }

View File

@ -12,7 +12,7 @@ import (
term term
os os
// runtime // runtime
sync // sync
// time // time
) )
@ -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 {
/* /*