module cache fixes; do not allow function names starting with _
parent
0796e1dd69
commit
2411b8d1e7
|
@ -10,9 +10,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
fn (v mut V) cc() {
|
fn (v mut V) cc() {
|
||||||
// build any thirdparty obj files
|
|
||||||
v.build_thirdparty_obj_files()
|
v.build_thirdparty_obj_files()
|
||||||
// Just create a C/JavaScript file and exit
|
// Just create a C/JavaScript file and exit
|
||||||
|
// for example: `v -o v.c compiler`
|
||||||
if v.out_name.ends_with('.c') || v.out_name.ends_with('.js') {
|
if v.out_name.ends_with('.c') || v.out_name.ends_with('.js') {
|
||||||
// Translating V code to JS by launching vjs
|
// Translating V code to JS by launching vjs
|
||||||
$if !js {
|
$if !js {
|
||||||
|
|
|
@ -47,7 +47,7 @@ fn new_cgen(out_name_c string) &CGen {
|
||||||
out_path: path
|
out_path: path
|
||||||
out: out
|
out: out
|
||||||
//buf: strings.new_builder(10000)
|
//buf: strings.new_builder(10000)
|
||||||
lines: _make(0, 1000, sizeof(string))
|
lines: make(0, 1000, sizeof(string))
|
||||||
}
|
}
|
||||||
return gen
|
return gen
|
||||||
}
|
}
|
||||||
|
|
|
@ -214,7 +214,7 @@ fn (p mut Parser) fn_decl() {
|
||||||
p.register_var(receiver)
|
p.register_var(receiver)
|
||||||
}
|
}
|
||||||
// +-/* methods
|
// +-/* methods
|
||||||
if p.tok == .plus || p.tok == .minus || p.tok == .mul {
|
if p.tok in [TokenKind.plus, .minus, .mul] {
|
||||||
f.name = p.tok.str()
|
f.name = p.tok.str()
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
|
@ -226,17 +226,21 @@ fn (p mut Parser) fn_decl() {
|
||||||
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.build_mode == .build_module {
|
if 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()
|
||||||
f.is_c = true
|
f.is_c = true
|
||||||
}
|
}
|
||||||
else if !p.pref.translated && !p.file_path.contains('view.v') {
|
else if !p.pref.translated {
|
||||||
if contains_capital(f.name) {
|
if contains_capital(f.name) {
|
||||||
p.error('function names cannot contain uppercase letters, use snake_case instead')
|
p.error('function names cannot contain uppercase letters, use snake_case instead')
|
||||||
}
|
}
|
||||||
|
if f.name[0] == `_` {
|
||||||
|
// TODO error
|
||||||
|
p.warn('function names cannot start with `_`')
|
||||||
|
}
|
||||||
if f.name.contains('__') {
|
if f.name.contains('__') {
|
||||||
p.error('function names cannot contain double underscores, use single underscores instead')
|
p.error('function names cannot contain double underscores, use single underscores instead')
|
||||||
}
|
}
|
||||||
|
@ -382,8 +386,9 @@ fn (p mut Parser) fn_decl() {
|
||||||
}
|
}
|
||||||
p.add_method(receiver_t.name, f)
|
p.add_method(receiver_t.name, f)
|
||||||
}
|
}
|
||||||
else {
|
else if p.first_pass(){
|
||||||
// println('register_fn typ=$typ isg=$is_generic')
|
// println('register_fn $f.name typ=$typ isg=$is_generic pass=$p.pass ' +
|
||||||
|
//'$p.file_name')
|
||||||
p.table.register_fn(f)
|
p.table.register_fn(f)
|
||||||
}
|
}
|
||||||
if p.is_vh || p.first_pass() || is_live || is_fn_header || skip_main_in_test {
|
if p.is_vh || p.first_pass() || is_live || is_fn_header || skip_main_in_test {
|
||||||
|
@ -807,7 +812,7 @@ fn (p mut Parser) fn_call_args(f mut Fn) &Fn {
|
||||||
file_path := p.file_path.replace('\\', '\\\\') // escape \
|
file_path := p.file_path.replace('\\', '\\\\') // escape \
|
||||||
p.cgen.resetln(p.cgen.cur_line.replace(
|
p.cgen.resetln(p.cgen.cur_line.replace(
|
||||||
'v_panic (',
|
'v_panic (',
|
||||||
'_panic_debug ($p.scanner.line_nr, tos2((byte *)"$file_path"), tos2((byte *)"$mod_name"), tos2((byte *)"$fn_name"), '
|
'panic_debug ($p.scanner.line_nr, tos3("$file_path"), tos3("$mod_name"), tos2((byte *)"$fn_name"), '
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
for i, arg in f.args {
|
for i, arg in f.args {
|
||||||
|
|
|
@ -785,12 +785,9 @@ fn new_v(args[]string) &V {
|
||||||
if args.len < 2 {
|
if args.len < 2 {
|
||||||
dir = ''
|
dir = ''
|
||||||
}
|
}
|
||||||
|
|
||||||
// println('new compiler "$dir"')
|
|
||||||
// build mode
|
// build mode
|
||||||
mut build_mode := BuildMode.default_mode
|
mut build_mode := BuildMode.default_mode
|
||||||
mut mod := ''
|
mut mod := ''
|
||||||
//if args.contains('-lib') {
|
|
||||||
if joined_args.contains('build module ') {
|
if joined_args.contains('build module ') {
|
||||||
build_mode = .build_module
|
build_mode = .build_module
|
||||||
// v build module ~/v/os => os.o
|
// v build module ~/v/os => os.o
|
||||||
|
|
|
@ -72,6 +72,18 @@ fn v_type_str(typ_ string) string {
|
||||||
typ_
|
typ_
|
||||||
}
|
}
|
||||||
typ = typ.replace('Option_', '?')
|
typ = typ.replace('Option_', '?')
|
||||||
|
if typ.contains('_V_MulRet') {
|
||||||
|
words := typ.replace('_V_MulRet_', '').split('_V_')
|
||||||
|
typ = '('
|
||||||
|
for i in 0 .. words.len {
|
||||||
|
typ += words[i]
|
||||||
|
if i != words.len - 1 {
|
||||||
|
typ += ','
|
||||||
|
}
|
||||||
|
}
|
||||||
|
typ += ')'
|
||||||
|
return typ
|
||||||
|
}
|
||||||
//println('"$typ"')
|
//println('"$typ"')
|
||||||
if typ == '*void' {
|
if typ == '*void' {
|
||||||
return 'voidptr'
|
return 'voidptr'
|
||||||
|
@ -132,13 +144,16 @@ fn (v &V) generate_vh() {
|
||||||
file.writeln('\n')
|
file.writeln('\n')
|
||||||
}
|
}
|
||||||
// Types
|
// Types
|
||||||
file.writeln('// Types2')
|
file.writeln('// Types')
|
||||||
for _, typ in v.table.typesmap {
|
for _, typ in v.table.typesmap {
|
||||||
//println(typ.name)
|
//println(typ.name)
|
||||||
if typ.mod != v.mod && typ.mod != ''{ // int, string etc mod == ''
|
if typ.mod != v.mod && typ.mod != ''{ // int, string etc mod == ''
|
||||||
//println('skipping type "$typ.name"')
|
//println('skipping type "$typ.name"')
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if typ.name.contains('_V_MulRet') {
|
||||||
|
continue
|
||||||
|
}
|
||||||
mut name := typ.name
|
mut name := typ.name
|
||||||
if typ.name.contains('__') {
|
if typ.name.contains('__') {
|
||||||
name = typ.name.all_after('__')
|
name = typ.name.all_after('__')
|
||||||
|
@ -179,7 +194,7 @@ fn (v &V) generate_vh() {
|
||||||
if f.mod == v.mod || f.mod == ''{
|
if f.mod == v.mod || f.mod == ''{
|
||||||
fns << f
|
fns << f
|
||||||
} else {
|
} else {
|
||||||
println('skipping fn $f.name mod=$f.mod')
|
//println('skipping fn $f.name mod=$f.mod')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, f in fns {
|
for _, f in fns {
|
||||||
|
@ -199,7 +214,7 @@ fn (v &V) generate_vh() {
|
||||||
file.writeln('\n// Methods //////////////////')
|
file.writeln('\n// Methods //////////////////')
|
||||||
for _, typ in v.table.typesmap {
|
for _, typ in v.table.typesmap {
|
||||||
if typ.mod != v.mod { //&& typ.mod != '' {
|
if typ.mod != v.mod { //&& typ.mod != '' {
|
||||||
println('skipping method typ $typ.name mod=$typ.mod')
|
//println('skipping method typ $typ.name mod=$typ.mod')
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for method in typ.methods {
|
for method in typ.methods {
|
||||||
|
|
|
@ -122,7 +122,7 @@ fn (v mut V) new_parser_from_file(path string) Parser {
|
||||||
is_vh: path.ends_with('.vh')
|
is_vh: path.ends_with('.vh')
|
||||||
}
|
}
|
||||||
if p.pref.building_v {
|
if p.pref.building_v {
|
||||||
p.scanner.should_print_relative_paths_on_error = false
|
p.scanner.should_print_relative_paths_on_error = true
|
||||||
}
|
}
|
||||||
v.cgen.file = path
|
v.cgen.file = path
|
||||||
p.scan_tokens()
|
p.scan_tokens()
|
||||||
|
|
|
@ -562,7 +562,7 @@ fn (t &Table) find_type(name_ string) Type {
|
||||||
return t.typesmap[name]
|
return t.typesmap[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) _check_types(got_, expected_ string, throw bool) bool {
|
fn (p mut Parser) check_types2(got_, expected_ string, throw bool) bool {
|
||||||
mut got := got_
|
mut got := got_
|
||||||
mut expected := expected_
|
mut expected := expected_
|
||||||
//p.log('check types got="$got" exp="$expected" ')
|
//p.log('check types got="$got" exp="$expected" ')
|
||||||
|
|
|
@ -29,7 +29,7 @@ fn new_array(mylen, cap, elm_size int) array {
|
||||||
|
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
pub fn _make(len, cap, elm_size int) array {
|
pub fn make(len, cap, elm_size int) array {
|
||||||
return new_array(len, cap, elm_size)
|
return new_array(len, cap, elm_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,6 +111,13 @@ pub fn (a mut array) delete(idx int) {
|
||||||
a.cap--
|
a.cap--
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn (a array) get(i int) voidptr {
|
||||||
|
if i < 0 || i >= a.len {
|
||||||
|
panic('array index out of range: $i/$a.len')
|
||||||
|
}
|
||||||
|
return a.data + i * a.element_size
|
||||||
|
}
|
||||||
|
|
||||||
fn (a array) _get(i int) voidptr {
|
fn (a array) _get(i int) voidptr {
|
||||||
if i < 0 || i >= a.len {
|
if i < 0 || i >= a.len {
|
||||||
panic('array index out of range: $i/$a.len')
|
panic('array index out of range: $i/$a.len')
|
||||||
|
@ -175,6 +182,22 @@ fn (a mut array) set(idx int, val voidptr) {
|
||||||
C.memcpy(a.data + a.element_size * idx, val, a.element_size)
|
C.memcpy(a.data + a.element_size * idx, val, a.element_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn (arr mut array) push(val voidptr) {
|
||||||
|
if arr.len >= arr.cap - 1 {
|
||||||
|
cap := (arr.len + 1) * 2
|
||||||
|
// println('_push: realloc, new cap=$cap')
|
||||||
|
if arr.cap == 0 {
|
||||||
|
arr.data = calloc(cap * arr.element_size)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
arr.data = C.realloc(arr.data, cap * arr.element_size)
|
||||||
|
}
|
||||||
|
arr.cap = cap
|
||||||
|
}
|
||||||
|
C.memcpy(arr.data + arr.element_size * arr.len, val, arr.element_size)
|
||||||
|
arr.len++
|
||||||
|
}
|
||||||
|
|
||||||
fn (arr mut array) _push(val voidptr) {
|
fn (arr mut array) _push(val voidptr) {
|
||||||
if arr.len >= arr.cap - 1 {
|
if arr.len >= arr.cap - 1 {
|
||||||
cap := (arr.len + 1) * 2
|
cap := (arr.len + 1) * 2
|
||||||
|
@ -209,6 +232,24 @@ pub fn (arr mut array) _push_many(val voidptr, size int) {
|
||||||
arr.len += size
|
arr.len += size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// `val` is array.data
|
||||||
|
// TODO make private, right now it's used by strings.Builder
|
||||||
|
pub fn (arr mut array) push_many(val voidptr, size int) {
|
||||||
|
if arr.len >= arr.cap - size {
|
||||||
|
cap := (arr.len + size) * 2
|
||||||
|
// println('_push: realloc, new cap=$cap')
|
||||||
|
if arr.cap == 0 {
|
||||||
|
arr.data = calloc(cap * arr.element_size)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
arr.data = C.realloc(arr.data, cap * arr.element_size)
|
||||||
|
}
|
||||||
|
arr.cap = cap
|
||||||
|
}
|
||||||
|
C.memcpy(arr.data + arr.element_size * arr.len, val, arr.element_size * size)
|
||||||
|
arr.len += size
|
||||||
|
}
|
||||||
|
|
||||||
pub fn (a array) reverse() array {
|
pub fn (a array) reverse() array {
|
||||||
arr := array {
|
arr := array {
|
||||||
len: a.len
|
len: a.len
|
||||||
|
|
|
@ -57,7 +57,7 @@ pub fn print_backtrace(){
|
||||||
}
|
}
|
||||||
|
|
||||||
// replaces panic when -debug arg is passed
|
// replaces panic when -debug arg is passed
|
||||||
fn _panic_debug(line_no int, file, mod, fn_name, s string) {
|
fn panic_debug(line_no int, file, mod, fn_name, s string) {
|
||||||
println('================ V panic ================')
|
println('================ V panic ================')
|
||||||
println(' module: $mod')
|
println(' module: $mod')
|
||||||
println(' function: ${fn_name}()')
|
println(' function: ${fn_name}()')
|
||||||
|
|
|
@ -47,7 +47,7 @@ fn new_hashmap(planned_nr_items int) hashmap {
|
||||||
return hashmap{
|
return hashmap{
|
||||||
cap: cap
|
cap: cap
|
||||||
elm_size: 4
|
elm_size: 4
|
||||||
table: _make(cap, cap, sizeof(hashmapentry))
|
table: make(cap, cap, sizeof(hashmapentry))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ pub fn _make(len, cap, elm_size int) array {
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pub fn _make(len, cap, elm_size int) array {
|
pub fn make(len, cap, elm_size int) array {
|
||||||
return array{}
|
return array{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -125,6 +125,15 @@ fn (m mut map) _set(key string, val voidptr) {
|
||||||
m.insert(mut m.root, key, val)
|
m.insert(mut m.root, key, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn (m mut map) set(key string, val voidptr) {
|
||||||
|
if isnil(m.root) {
|
||||||
|
m.root = new_node(key, val, m.element_size)
|
||||||
|
m.size++
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m.insert(mut m.root, key, val)
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
fn (m map) bs(query string, start, end int, out voidptr) {
|
fn (m map) bs(query string, start, end int, out voidptr) {
|
||||||
// println('bs "$query" $start -> $end')
|
// println('bs "$query" $start -> $end')
|
||||||
|
@ -210,8 +219,8 @@ pub fn (m mut map) delete(key string) {
|
||||||
m.size--
|
m.size--
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (m map) exists(key string) {
|
fn (m map) exists(key string) bool {
|
||||||
panic('map.exists(key) was removed from the language. Use `key in map` instead.')
|
return !isnil(m.root) && m.root.find2(key, m.element_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (m map) _exists(key string) bool {
|
fn (m map) _exists(key string) bool {
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub:
|
||||||
|
|
||||||
pub fn new_builder(initial_size int) Builder {
|
pub fn new_builder(initial_size int) Builder {
|
||||||
return Builder {
|
return Builder {
|
||||||
buf: _make(0, initial_size, sizeof(byte))
|
buf: make(0, initial_size, sizeof(byte))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub:
|
||||||
|
|
||||||
pub fn new_builder(initial_size int) Builder {
|
pub fn new_builder(initial_size int) Builder {
|
||||||
return Builder {
|
return Builder {
|
||||||
buf: _make(0, initial_size, sizeof(byte))
|
buf: make(0, initial_size, sizeof(byte))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,11 @@
|
||||||
|
|
||||||
module term
|
module term
|
||||||
|
|
||||||
fn _format(msg, open, close string) string {
|
pub fn format(msg, open, close string) string {
|
||||||
return '\x1b[' + open + 'm' + msg + '\x1b[' + close + 'm'
|
return '\x1b[' + open + 'm' + msg + '\x1b[' + close + 'm'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _format_rgb(r, g, b int, msg, open, close string) string {
|
pub fn format_rgb(r, g, b int, msg, open, close string) string {
|
||||||
return '\x1b[' + open + ';2;' + r.str() + ';' + g.str() + ';' + b.str() + 'm' + msg + '\x1b[' + close + 'm'
|
return '\x1b[' + open + ';2;' + r.str() + ';' + g.str() + ';' + b.str() + 'm' + msg + '\x1b[' + close + 'm'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,3 @@
|
||||||
|
|
||||||
module term
|
module term
|
||||||
|
|
||||||
pub fn format(msg, open, close string) string {
|
|
||||||
return _format(msg, open, close)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn format_rgb(r, g, b int, msg, open, close string) string {
|
|
||||||
return _format_rgb(r, g, b, msg, open, close)
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,10 +4,3 @@
|
||||||
|
|
||||||
module term
|
module term
|
||||||
|
|
||||||
pub fn format(msg, open, close string) string {
|
|
||||||
return _format(msg, open, close)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn format_rgb(r, g, b int, msg, open, close string) string {
|
|
||||||
return _format_rgb(r, g, b, msg, open, close)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue