instead of `Enum.foo` require `.foo` if it's enough + some UI fixes

pull/2592/head
Alexander Medvednikov 2019-10-30 15:04:25 +03:00
parent 9abbfa7862
commit 96f7620628
7 changed files with 36 additions and 28 deletions

View File

@ -215,9 +215,9 @@ fn (p mut Parser) chash() {
else if hash.contains('embed') { else if hash.contains('embed') {
pos := hash.index('embed') + 5 pos := hash.index('embed') + 5
file := hash[pos..] file := hash[pos..]
if p.pref.build_mode != .default_mode { //if p.pref.build_mode != .default_mode {
p.genln('#include $file') p.genln('#include $file')
} //}
} }
else if hash.contains('define') { else if hash.contains('define') {
// Move defines on top // Move defines on top

View File

@ -262,7 +262,7 @@ fn (p mut Parser) fn_decl() {
p.register_var(receiver) p.register_var(receiver)
} }
// +-/* methods // +-/* methods
if p.tok in [TokenKind.plus, .minus, .mul] { if p.tok in [.plus, .minus, .mul] {
f.name = p.tok.str() f.name = p.tok.str()
p.next() p.next()
} }
@ -277,9 +277,9 @@ fn (p mut Parser) fn_decl() {
// C function header def? (fn C.NSMakeRect(int,int,int,int)) // C function header def? (fn C.NSMakeRect(int,int,int,int))
is_c := f.name == 'C' && p.tok == .dot is_c := f.name == 'C' && p.tok == .dot
// Just fn signature? only builtin.v + default build mode // Just fn signature? only builtin.v + default build mode
if p.pref.is_verbose { // p.pref.build_mode == .build_module { //if p.pref.is_verbose { // p.pref.build_mode == .build_module {
println('\n\nfn_decl() name=$f.name receiver_typ=$receiver_typ nogen=$p.cgen.nogen') //println('\n\nfn_decl() name=$f.name receiver_typ=$receiver_typ nogen=$p.cgen.nogen')
} //}
if is_c { if is_c {
p.check(.dot) p.check(.dot)
f.name = p.check_name() f.name = p.check_name()
@ -348,7 +348,7 @@ fn (p mut Parser) fn_decl() {
} }
// Returns a type? // Returns a type?
mut typ := 'void' mut typ := 'void'
if p.tok in [TokenKind.name, .mul, .amp, .lsbr, .question, .lpar] { if p.tok in [.name, .mul, .amp, .lsbr, .question, .lpar] {
p.fgen(' ') p.fgen(' ')
typ = p.get_type() typ = p.get_type()
} }

View File

@ -204,7 +204,7 @@ pub fn (v mut V) compile() {
} }
// Main pass // Main pass
cgen.pass = Pass.main cgen.pass = .main
if v.pref.is_debug { if v.pref.is_debug {
$if js { $if js {
cgen.genln('const VDEBUG = 1;\n') cgen.genln('const VDEBUG = 1;\n')

View File

@ -46,6 +46,8 @@ fn generate_vh(mod string) {
//} //}
filtered := vfiles.filter(it.ends_with('.v') && !it.ends_with('test.v') && filtered := vfiles.filter(it.ends_with('.v') && !it.ends_with('test.v') &&
!it.ends_with('_windows.v') && !it.ends_with('_win.v') && !it.ends_with('_windows.v') && !it.ends_with('_win.v') &&
!it.ends_with('_lin.v') &&
!it.contains('/examples') &&
!it.contains('/js')) // TODO merge once filter allows it !it.contains('/js')) // TODO merge once filter allows it
println(filtered) println(filtered)
mut v := new_v(['foo.v']) mut v := new_v(['foo.v'])
@ -62,9 +64,9 @@ fn generate_vh(mod string) {
continue continue
} }
match tok.tok { match tok.tok {
TokenKind.key_fn { fns.writeln(generate_fn(p.tokens, i)) } .key_fn { fns.writeln(generate_fn(p.tokens, i)) }
TokenKind.key_const { consts.writeln(generate_const(p.tokens, i)) } .key_const { consts.writeln(generate_const(p.tokens, i)) }
TokenKind.key_struct { types.writeln(generate_type(p.tokens, i)) } .key_struct { types.writeln(generate_type(p.tokens, i)) }
} }
} }
} }

View File

@ -197,7 +197,7 @@ fn (p mut Parser) next() {
p.prev_tok = p.tok p.prev_tok = p.tok
p.scanner.prev_tok = p.tok p.scanner.prev_tok = p.tok
if p.token_idx >= p.tokens.len { if p.token_idx >= p.tokens.len {
p.tok = TokenKind.eof p.tok = .eof
p.lit = '' p.lit = ''
return return
} }
@ -211,7 +211,7 @@ fn (p mut Parser) next() {
fn (p & Parser) peek() TokenKind { fn (p & Parser) peek() TokenKind {
if p.token_idx >= p.tokens.len - 2 { if p.token_idx >= p.tokens.len - 2 {
return TokenKind.eof return .eof
} }
tok := p.tokens[p.token_idx] tok := p.tokens[p.token_idx]
return tok.tok return tok.tok
@ -371,7 +371,8 @@ fn (p mut Parser) parse(pass Pass) {
} }
.key_global { .key_global {
if !p.pref.translated && !p.pref.is_live && if !p.pref.translated && !p.pref.is_live &&
!p.builtin_mod && !p.pref.building_v && !os.getwd().contains('/volt') { !p.builtin_mod && !p.pref.building_v &&
p.mod != 'ui' && !os.getwd().contains('/volt') {
p.error('__global is only allowed in translated code') p.error('__global is only allowed in translated code')
} }
p.next() p.next()
@ -760,7 +761,7 @@ fn (p mut Parser) get_type() string {
p.fn_args(mut f) p.fn_args(mut f)
// Same line, it's a return type // Same line, it's a return type
if p.scanner.line_nr == line_nr { if p.scanner.line_nr == line_nr {
if p.tok in [TokenKind.name, .mul, .amp, .lsbr, .question, .lpar] { if p.tok in [.name, .mul, .amp, .lsbr, .question, .lpar] {
f.typ = p.get_type() f.typ = p.get_type()
} }
else { else {
@ -1639,6 +1640,9 @@ fn (p mut Parser) name_expr() string {
if !enum_type.has_enum_val(val) { if !enum_type.has_enum_val(val) {
p.error('enum `$enum_type.name` does not have value `$val`') p.error('enum `$enum_type.name` does not have value `$val`')
} }
if p.expected_type == enum_type.name {
p.warn('`${enum_type.name}.$val` is unnecessary, use `.$val`')
}
// println('enum val $val') // println('enum val $val')
p.gen(mod_gen_name(enum_type.mod) + '__' + enum_type.name + '_' + val)// `color = main__Color_green` p.gen(mod_gen_name(enum_type.mod) + '__' + enum_type.name + '_' + val)// `color = main__Color_green`
@ -2374,7 +2378,7 @@ fn (p mut Parser) expression() string {
return 'int' return 'int'
} }
// + - | ^ // + - | ^
for p.tok in [TokenKind.plus, .minus, .pipe, .amp, .xor] { for p.tok in [.plus, .minus, .pipe, .amp, .xor] {
tok_op := p.tok tok_op := p.tok
if typ == 'bool' { if typ == 'bool' {
p.error('operator ${p.tok.str()} not defined on bool ') p.error('operator ${p.tok.str()} not defined on bool ')
@ -3604,9 +3608,9 @@ if (!$tmp) {
p.genln(';\n p.genln(';\n
if (!$tmp) { if (!$tmp) {
g_test_fails++; g_test_fails++;
main__cb_assertion_failed( main__cb_assertion_failed(
tos3("$filename"), tos3("$filename"),
$p.scanner.line_nr, $p.scanner.line_nr,
tos3("$sourceline"), tos3("$sourceline"),
tos3("$p.cur_fn.name()") tos3("$p.cur_fn.name()")
); );
@ -3615,9 +3619,9 @@ if (!$tmp) {
// Maybe print all vars in a test function if it fails? // Maybe print all vars in a test function if it fails?
} else { } else {
g_test_oks++; g_test_oks++;
main__cb_assertion_ok( main__cb_assertion_ok(
tos3("$filename"), tos3("$filename"),
$p.scanner.line_nr, $p.scanner.line_nr,
tos3("$sourceline"), tos3("$sourceline"),
tos3("$p.cur_fn.name()") tos3("$p.cur_fn.name()")
); );

View File

@ -257,17 +257,17 @@ fn (t TokenKind) str() string {
} }
fn (t TokenKind) is_decl() bool { fn (t TokenKind) is_decl() bool {
return t in [TokenKind.key_enum, .key_interface, .key_fn, return t in [.key_enum, .key_interface, .key_fn,
.key_struct ,.key_type, .key_const, .key_import_const, .key_pub, .eof] .key_struct ,.key_type, .key_const, .key_import_const, .key_pub, .eof]
} }
const ( const (
AssignTokens = [ AssignTokens = [
TokenKind.assign, TokenKind.plus_assign, TokenKind.minus_assign, TokenKind.assign, .plus_assign, .minus_assign,
TokenKind.mult_assign, TokenKind.div_assign, TokenKind.xor_assign, .mult_assign, .div_assign, .xor_assign,
TokenKind.mod_assign, .mod_assign,
TokenKind.or_assign, TokenKind.and_assign, TokenKind.righ_shift_assign, .or_assign, .and_assign, .righ_shift_assign,
TokenKind.left_shift_assign .left_shift_assign
] ]
) )

View File

@ -6,6 +6,8 @@ module ui
#include <Cocoa/Cocoa.h> #include <Cocoa/Cocoa.h>
#include <Carbon/Carbon.h> #include <Carbon/Carbon.h>
__global default_font *C.NSFont
fn focus_app(next, event, data voidptr) { fn focus_app(next, event, data voidptr) {
#NSLog(@"2The hot key was pressed."); #NSLog(@"2The hot key was pressed.");