1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

run vfmt on more files

This commit is contained in:
Alexander Medvednikov 2019-12-18 21:56:30 +03:00
parent 1cef83aea4
commit df2f98e276
8 changed files with 403 additions and 320 deletions

View File

@ -1,11 +1,9 @@
// Copyright (c) 2019 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 builtin
#include <float.h>
pub fn (d f64) str() string {
buf := malloc(sizeof(double) * 5 + 1) // TODO
C.sprintf(charptr(buf), '%f', d)
@ -18,7 +16,6 @@ pub fn (d f32) str() string {
return tos(buf, vstrlen(buf))
}
// return a string of the input f64 in scientific notation with digit_num digits displayed
pub fn (x f64) strsci(digit_num int) string {
buf := malloc(digit_num * 2 + 2) // TODO
@ -31,19 +28,25 @@ pub fn (x f64) strsci(digit_num int) string{
// return a long string of the input f64, max
pub fn (x f64) strlong() string {
buf := malloc(18 + 32) // TODO
C.sprintf(charptr(buf),"%0.30lf",x)
C.sprintf(charptr(buf), '%0.30lf', x)
tmpstr := tos(buf, vstrlen(buf))
return tmpstr
}
fn f32_abs(a f32) f32 { return if a < 0 { -a } else { a } }
fn f64_abs(a f64) f64 { return if a < 0 { -a } else { a } }
fn f32_abs(a f32) f32 {
return if a < 0 { -a } else { a }
}
fn f64_abs(a f64) f64 {
return if a < 0 { -a } else { a }
}
// compare floats using C epsilon
// ==
pub fn (a f64) eq(b f64) bool {
return f64_abs(a - b) <= C.DBL_EPSILON
}
pub fn (a f32) eq(b f32) bool {
return f32_abs(a - b) <= C.FLT_EPSILON
}
@ -60,12 +63,15 @@ pub fn (a f32) eqbit(b f32) bool {
fn (a f64) ne(b f64) bool {
return !a.eq(b)
}
fn (a f32) ne(b f32) bool {
return !a.eq(b)
}
pub fn (a f64) nebit(b f64) bool {
return C.DEFAULT_NOT_EQUAL(a, b)
}
pub fn (a f32) nebit(b f32) bool {
return C.DEFAULT_NOT_EQUAL(a, b)
}
@ -74,12 +80,15 @@ pub fn (a f32) nebit(b f32) bool {
fn (a f64) lt(b f64) bool {
return a.ne(b) && a.ltbit(b)
}
fn (a f32) lt(b f32) bool {
return a.ne(b) && a.ltbit(b)
}
fn (a f64) ltbit(b f64) bool {
return C.DEFAULT_LT(a, b)
}
fn (a f32) ltbit(b f32) bool {
return C.DEFAULT_LT(a, b)
}
@ -88,12 +97,15 @@ fn (a f32) ltbit(b f32) bool {
fn (a f64) le(b f64) bool {
return !a.gt(b)
}
fn (a f32) le(b f32) bool {
return !a.gt(b)
}
fn (a f64) lebit(b f64) bool {
return C.DEFAULT_LE(a, b)
}
fn (a f32) lebit(b f32) bool {
return C.DEFAULT_LE(a, b)
}
@ -102,12 +114,15 @@ fn (a f32) lebit(b f32) bool {
fn (a f64) gt(b f64) bool {
return a.ne(b) && a.gtbit(b)
}
fn (a f32) gt(b f32) bool {
return a.ne(b) && a.gtbit(b)
}
fn (a f64) gtbit(b f64) bool {
return C.DEFAULT_GT(a, b)
}
fn (a f32) gtbit(b f32) bool {
return C.DEFAULT_GT(a, b)
}
@ -116,16 +131,15 @@ fn (a f32) gtbit(b f32) bool {
fn (a f64) ge(b f64) bool {
return !a.lt(b)
}
fn (a f32) ge(b f32) bool {
return !a.lt(b)
}
fn (a f64) gebit(b f64) bool {
return C.DEFAULT_GE(a, b)
}
fn (a f32) gebit(b f32) bool {
return C.DEFAULT_GE(a, b)
}

View File

@ -199,6 +199,7 @@ fn (p mut Parser) comp_time() {
fn (p mut Parser) chash() {
hash := p.lit.trim_space()
// println('chsh() file=$p.file hash="$hash"')
p.fgen_nl()
p.next()
if hash.starts_with('flag ') {
if p.first_pass() {

View File

@ -1,7 +1,6 @@
// Copyright (c) 2019 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 compiler
fn (p mut Parser) bool_expression() string {
@ -12,11 +11,15 @@ fn (p mut Parser) bool_expression() string {
for p.tok == .and || p.tok == .logical_or {
if p.tok == .and {
got_and = true
if got_or { p.error(and_or_error) }
if got_or {
p.error(and_or_error)
}
}
if p.tok == .logical_or {
got_or = true
if got_and { p.error(and_or_error) }
if got_and {
p.error(and_or_error)
}
}
if p.is_sql {
if p.tok == .and {
@ -25,7 +28,8 @@ fn (p mut Parser) bool_expression() string {
else if p.tok == .logical_or {
p.gen(' or ')
}
} else {
}
else {
p.gen(' ${p.tok.str()} ')
}
p.check_space(p.tok)
@ -50,9 +54,7 @@ fn (p mut Parser) bterm() string {
is_str := typ == 'string' && !p.is_sql
is_ustr := typ == 'ustring'
base := p.base_type(typ)
is_float := base[0] == `f` && (base in ['f64', 'f32']) &&
!(p.cur_fn.name in ['f64_abs', 'f32_abs']) &&
p.cur_fn.name != 'eq'
is_float := base[0] == `f` && (base in ['f64', 'f32']) && !(p.cur_fn.name in ['f64_abs', 'f32_abs']) && p.cur_fn.name != 'eq'
is_array := typ.starts_with('array_')
expr_type := base
tok := p.tok
@ -61,12 +63,12 @@ fn (p mut Parser) bterm() string {
p.error('no = ')
}
*/
if tok in [.eq, .gt, .lt, .le, .ge, .ne] {
// TODO: remove when array comparing is supported
if is_array {
p.error('array comparison is not supported yet')
}
p.fspace()
// p.fgen(' ${p.tok.str()} ')
if (is_float || is_str || is_ustr) && !p.is_js {
@ -90,45 +92,83 @@ fn (p mut Parser) bterm() string {
p.sql_params << sql_param
p.sql_types << typ
// println('*** sql type: $typ | param: $sql_param')
} else {
}
else {
p.check_types(p.expression(), typ)
}
typ = 'bool'
if is_str && !p.is_js { //&& !p.is_sql {
if is_str && !p.is_js {
// && !p.is_sql {
p.gen(')')
match tok {
.eq { p.cgen.set_placeholder(ph, 'string_eq(') }
.ne { p.cgen.set_placeholder(ph, 'string_ne(') }
.le { p.cgen.set_placeholder(ph, 'string_le(') }
.ge { p.cgen.set_placeholder(ph, 'string_ge(') }
.gt { p.cgen.set_placeholder(ph, 'string_gt(') }
.lt { p.cgen.set_placeholder(ph, 'string_lt(') }
else { }
.eq {
p.cgen.set_placeholder(ph, 'string_eq(')
}
.ne {
p.cgen.set_placeholder(ph, 'string_ne(')
}
.le {
p.cgen.set_placeholder(ph, 'string_le(')
}
.ge {
p.cgen.set_placeholder(ph, 'string_ge(')
}
.gt {
p.cgen.set_placeholder(ph, 'string_gt(')
}
.lt {
p.cgen.set_placeholder(ph, 'string_lt(')
}
else {
}}
}
if is_ustr {
p.gen(')')
match tok {
.eq { p.cgen.set_placeholder(ph, 'ustring_eq(') }
.ne { p.cgen.set_placeholder(ph, 'ustring_ne(') }
.le { p.cgen.set_placeholder(ph, 'ustring_le(') }
.ge { p.cgen.set_placeholder(ph, 'ustring_ge(') }
.gt { p.cgen.set_placeholder(ph, 'ustring_gt(') }
.lt { p.cgen.set_placeholder(ph, 'ustring_lt(') }
else { }
.eq {
p.cgen.set_placeholder(ph, 'ustring_eq(')
}
.ne {
p.cgen.set_placeholder(ph, 'ustring_ne(')
}
.le {
p.cgen.set_placeholder(ph, 'ustring_le(')
}
.ge {
p.cgen.set_placeholder(ph, 'ustring_ge(')
}
.gt {
p.cgen.set_placeholder(ph, 'ustring_gt(')
}
.lt {
p.cgen.set_placeholder(ph, 'ustring_lt(')
}
else {
}}
}
if is_float && p.cur_fn.name != 'f32_abs' && p.cur_fn.name != 'f64_abs' {
p.gen(')')
match tok {
.eq { p.cgen.set_placeholder(ph, '${expr_type}_eq(') }
.ne { p.cgen.set_placeholder(ph, '${expr_type}_ne(') }
.le { p.cgen.set_placeholder(ph, '${expr_type}_le(') }
.ge { p.cgen.set_placeholder(ph, '${expr_type}_ge(') }
.gt { p.cgen.set_placeholder(ph, '${expr_type}_gt(') }
.lt { p.cgen.set_placeholder(ph, '${expr_type}_lt(') }
else { }
.eq {
p.cgen.set_placeholder(ph, '${expr_type}_eq(')
}
.ne {
p.cgen.set_placeholder(ph, '${expr_type}_ne(')
}
.le {
p.cgen.set_placeholder(ph, '${expr_type}_le(')
}
.ge {
p.cgen.set_placeholder(ph, '${expr_type}_ge(')
}
.gt {
p.cgen.set_placeholder(ph, '${expr_type}_gt(')
}
.lt {
p.cgen.set_placeholder(ph, '${expr_type}_lt(')
}
else {
}}
}
}
return typ
@ -147,14 +187,15 @@ fn (p mut Parser) name_expr() string {
for {
if p.tok == .amp {
mul_nr++
}else if p.tok == .mul {
}
else if p.tok == .mul {
deref_nr++
}else {
}
else {
break
}
p.next()
}
if p.tok == .lpar {
p.gen('*'.repeat(deref_nr))
p.gen('(')
@ -194,10 +235,10 @@ fn (p mut Parser) name_expr() string {
p.check(.dot)
name = p.lit
// C struct initialization
if p.peek() == .lcbr && p.expected_type == '' { // not an expression
if p.peek() == .lcbr && p.expected_type == '' {
// not an expression
if !p.table.known_type(name) {
p.error('unknown C type `$name`, ' +
'define it with `struct C.$name { ... }`')
p.error('unknown C type `$name`, ' + 'define it with `struct C.$name { ... }`')
}
return p.get_struct_type(name, true, ptr)
}
@ -232,8 +273,9 @@ fn (p mut Parser) name_expr() string {
p.check_enum_member_access()
// println("found enum value: $p.expected_type")
return p.expected_type
} else {
p.error("unknown enum: `$p.expected_type`")
}
else {
p.error('unknown enum: `$p.expected_type`')
}
}
// Variable, checked before modules, so that module shadowing is allowed:
@ -242,9 +284,7 @@ fn (p mut Parser) name_expr() string {
return p.get_var_type(name, ptr, deref_nr)
}
// Module?
if p.peek() == .dot && (name == p.mod ||
p.import_table.known_alias(name)) && !is_c
{
if p.peek() == .dot && (name == p.mod || p.import_table.known_alias(name)) && !is_c {
mut mod := name
// must be aliased module
if name != p.mod && p.import_table.known_alias(name) {
@ -258,9 +298,7 @@ fn (p mut Parser) name_expr() string {
}
// Unknown name, try prepending the module name to it
// TODO perf
else if !p.table.known_type(name) &&
!p.table.known_fn(name) && !p.table.known_const(name) && !is_c
{
else if !p.table.known_type(name) && !p.table.known_fn(name) && !p.table.known_const(name) && !is_c {
name = p.prepend_mod(name)
}
// re-check
@ -315,7 +353,6 @@ fn (p mut Parser) name_expr() string {
return p.get_struct_type(name, false, ptr)
}
}
// Constant
if p.table.known_const(name) {
return p.get_const_type(name, ptr)
@ -330,7 +367,8 @@ fn (p mut Parser) name_expr() string {
}
// exhaused all options type,enum,const,mod,var,fn etc
// so show undefined error (also checks typos)
p.undefined_error(name, orig_name) return '' // panics
p.undefined_error(name, orig_name)
return '' // panics
}
// no () after func, so func is an argument, just gen its name
// TODO verify this and handle errors
@ -339,6 +377,7 @@ fn (p mut Parser) name_expr() string {
// Register anon fn type
fn_typ := Type{
name: f.typ_str() // 'fn (int, int) string'
mod: p.mod
func: f
}
@ -351,7 +390,6 @@ fn (p mut Parser) name_expr() string {
if f.typ == 'void' && !p.inside_if_expr {
// p.error('`$f.name` used as value')
}
fn_call_ph := p.cgen.add_placeholder()
// println('call to fn $f.name of type $f.typ')
// TODO replace the following dirty hacks (needs ptr access to fn table)
@ -365,7 +403,6 @@ fn (p mut Parser) name_expr() string {
// println(' from $f2.name(${f2.str_args(p.table)}) $f2.typ : $f2.type_inst')
}
f = new_f
// optional function call `function() or {}`, no return assignment
is_or_else := p.tok == .key_orelse
if p.tok == .question {
@ -375,12 +412,10 @@ fn (p mut Parser) name_expr() string {
else if !p.is_var_decl && is_or_else {
f.typ = p.gen_handle_option_or_else(f.typ, '', fn_call_ph)
}
else if !p.is_var_decl && !is_or_else && !p.inside_return_expr &&
f.typ.starts_with('Option_') {
else if !p.is_var_decl && !is_or_else && !p.inside_return_expr && f.typ.starts_with('Option_') {
opt_type := f.typ[7..]
p.error('unhandled option type: `?$opt_type`')
}
// dot after a function call: `get_user().age`
if p.tok == .dot {
mut typ := ''
@ -391,7 +426,6 @@ fn (p mut Parser) name_expr() string {
return typ
}
// p.log('end of name_expr')
if f.typ.ends_with('*') {
p.is_alloc = true
}
@ -422,11 +456,10 @@ fn (p mut Parser) expression() string {
p.gen(', (')
// Immutable? Can we push?
if !p.expr_var.is_mut && !p.pref.translated {
p.error('`$p.expr_var.name` is immutable (can\'t <<)')
p.error("`$p.expr_var.name` is immutable (can\'t <<)")
}
if p.expr_var.is_arg && p.expr_var.typ.starts_with('array_') {
p.error("for now it's not possible to append an element to "+
'a mutable array argument `$p.expr_var.name`')
p.error("for now it's not possible to append an element to " + 'a mutable array argument `$p.expr_var.name`')
}
if !p.expr_var.is_changed {
p.mark_var_changed(p.expr_var)
@ -490,7 +523,8 @@ fn (p mut Parser) expression() string {
// Msvc errors on void* pointer arithmatic
// ... So cast to byte* and then do the add
p.cgen.set_placeholder(ph, '(byte*)')
}else if typ.contains('*') {
}
else if typ.contains('*') {
p.cgen.set_placeholder(ph, '($typ)')
}
p.gen(tok_op.str())
@ -528,8 +562,12 @@ fn (p mut Parser) expression() string {
// Make sure operators are used with correct types
if !p.pref.translated && !is_str && !is_ustr && !is_num {
T := p.table.find_type(typ)
if tok_op == .plus { p.handle_operator('+', typ, 'op_plus', ph, T) }
else if tok_op == .minus { p.handle_operator('-', typ, 'op_minus', ph, T) }
if tok_op == .plus {
p.handle_operator('+', typ, 'op_plus', ph, T)
}
else if tok_op == .minus {
p.handle_operator('-', typ, 'op_minus', ph, T)
}
}
}
return typ
@ -584,19 +622,25 @@ fn (p mut Parser) term() string {
after_oph := p.cgen.cur_line[oph..]
p.cgen.cur_line = before_oph + ',' + after_oph
match tok {
.mul { p.handle_operator('*', typ, 'op_mul', ph, T) }
.div { p.handle_operator('/', typ, 'op_div', ph, T) }
.mod { p.handle_operator('%', typ, 'op_mod', ph, T) }
else {}
.mul {
p.handle_operator('*', typ, 'op_mul', ph, T)
}
.div {
p.handle_operator('/', typ, 'op_div', ph, T)
}
.mod {
p.handle_operator('%', typ, 'op_mod', ph, T)
}
else {
}}
continue
}
if is_mod {
if !(is_integer_type(expr_type) && is_integer_type(typ)) {
p.error('operator `mod` requires integer types')
}
} else {
}
else {
p.check_types(expr_type, typ)
}
}
@ -623,8 +667,7 @@ fn (p mut Parser) unary() string {
}
else {
typ = p.factor()
}
}
}}
return typ
}
@ -643,11 +686,11 @@ fn (p mut Parser) factor() string {
.number {
typ = 'int'
// Check if float (`1.0`, `1e+3`) but not if is hexa
if (p.lit.contains('.') || (p.lit.contains('e') || p.lit.contains('E'))) &&
!(p.lit[0] == `0` && (p.lit[1] == `x` || p.lit[1] == `X`)) {
if (p.lit.contains('.') || (p.lit.contains('e') || p.lit.contains('E'))) && !(p.lit[0] == `0` && (p.lit[1] == `x` || p.lit[1] == `X`)) {
typ = 'f32'
// typ = 'f64' // TODO
} else {
}
else {
v_u64 := p.lit.u64()
if u64(u32(v_u64)) < v_u64 {
typ = 'u64'
@ -666,13 +709,13 @@ fn (p mut Parser) factor() string {
}
.key_sizeof {
p.gen('sizeof(')
p.fgen('sizeof(')
// p.fgen('sizeof(')
p.next()
p.check(.lpar)
mut sizeof_typ := p.get_type()
p.check(.rpar)
p.gen('$sizeof_typ)')
p.fgen('$sizeof_typ)')
// p.fgen('$sizeof_typ)')
return 'int'
}
.amp,.dot,.mul {
@ -710,6 +753,7 @@ fn (p mut Parser) factor() string {
return 'T'
}
*/
.lpar {
// p.gen('(/*lpar*/')
p.gen('(')
@ -772,8 +816,7 @@ fn (p mut Parser) factor() string {
println('next=${next.str()}')
}
p.error('unexpected token: `${p.tok.str()}`')
}
}
}}
p.next() // TODO everything should next()
return typ
}

View File

@ -277,6 +277,9 @@ fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
}
p.fspace()
p.check(.lcbr)
if p.inside_if_expr {
p.fspace()
}
mut typ := ''
// if { if hack
if p.tok == .key_if && p.inside_if_expr {
@ -289,7 +292,9 @@ fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
if_returns := p.returns
p.returns = false
if p.tok == .key_else {
if !p.inside_if_expr {
if p.inside_if_expr {
p.fspace()
} else {
p.fgen_nl()
}
p.check(.key_else)
@ -318,6 +323,9 @@ fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
p.genln(' else { ')
}
p.check(.lcbr)
if is_expr {
p.fspace()
}
// statements() returns the type of the last statement
first_typ := typ
typ = p.statements()

View File

@ -7,6 +7,7 @@ import (
os
strings
compiler.x64
//time
)
struct Parser {
@ -212,7 +213,9 @@ fn (v mut V) new_parser(scanner &Scanner) Parser {
return p
}
// __global scan_time i64
fn (p mut Parser) scan_tokens() {
//t := time.ticks()
for {
res := p.scanner.scan()
p.tokens << Token{
@ -225,6 +228,8 @@ fn (p mut Parser) scan_tokens() {
break
}
}
// scan_time += time.ticks() - t
// println('scan tokens $p.file_name $scan_time ')
}
fn (p mut Parser) set_current_fn(f Fn) {
@ -866,6 +871,9 @@ fn (p &Parser) strtok() string {
return "'$p.lit'"
}
}
if p.tok == .hash {
return '#' + p.lit
}
res := p.tok.str()
if res == '' {
n := int(p.tok)
@ -1165,6 +1173,9 @@ fn (p mut Parser) statements_no_rcbr() string {
}
}
// p.next()
if p.inside_if_expr {
p.fspace()
}
p.check(.rcbr)
// p.fmt_dec()
p.close_scope()

View File

@ -1,7 +1,6 @@
// Copyright (c) 2019 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 compiler
fn (p mut Parser) string_expr() {
@ -19,6 +18,7 @@ fn (p mut Parser) string_expr() {
Calling a C function sometimes requires a call to a string method
C.fun('ssss'.to_wide()) => fun(string_to_wide(tos3("ssss")))
*/
if (p.calling_c && p.peek() != .dot) || is_cstr || (p.pref.translated && p.mod == 'main') {
p.gen('"$f"')
}
@ -81,7 +81,8 @@ fn (p mut Parser) string_expr() {
cformat += '.'
p.next()
}
if p.tok == .minus { // support for left aligned formatting
if p.tok == .minus {
// support for left aligned formatting
cformat += '-'
p.next()
}
@ -151,4 +152,3 @@ fn (p mut Parser) string_expr() {
p.gen('_STR($format$args)')
}
}

View File

@ -243,7 +243,7 @@ fn (p &Parser) gen_fmt() {
if s == '' {
return
}
if !p.file_name.contains('parser.v') {return}
//if !p.file_name.contains('float.v') {return}
path := os.tmpdir() + '/' + p.file_name
println('generating ${path}')
mut out := os.create(path) or {

View File

@ -1,6 +1,6 @@
module strings
#-js
//#-js
// use levenshtein distance algorithm to calculate
// the distance between between two strings (lower is closer)
@ -14,7 +14,8 @@ pub fn levenshtein_distance(a, b string) int {
mut mn := if f[j] + 1 <= f[j - 1] + 1 { f[j] + 1 } else { f[j - 1] + 1 }
if cb != ca {
mn = if mn <= fj1 + 1 { mn } else { fj1 + 1 }
} else {
}
else {
mn = if mn <= fj1 { mn } else { fj1 }
}
fj1 = f[j]
@ -37,16 +38,21 @@ pub fn levenshtein_distance_percentage(a, b string) f32 {
// find the similarity between two strings.
// returns coefficient between 0.0 (not similar) and 1.0 (exact match).
pub fn dice_coefficient(s1, s2 string) f32 {
if s1.len == 0 || s2.len == 0 { return 0.0 }
if s1 == s2 { return 1.0 }
if s1.len < 2 || s2.len < 2 { return 0.0 }
if s1.len == 0 || s2.len == 0 {
return 0.0
}
if s1 == s2 {
return 1.0
}
if s1.len < 2 || s2.len < 2 {
return 0.0
}
a := if s1.len > s2.len { s1 } else { s2 }
b := if a == s1 { s2 } else { s1 }
mut first_bigrams := map[string]int
for i := 0; i < a.len - 1; i++ {
bigram := a[i..i + 2]
q := if bigram in first_bigrams {
first_bigrams[bigram]+1 } else { 1 }
q := if bigram in first_bigrams { first_bigrams[bigram] + 1 } else { 1 }
first_bigrams[bigram] = q
}
mut intersection_size := 0