replace ugly `tos2(bytes)` with `string(bytes)`

pull/702/head
Alexander Medvednikov 2019-06-27 12:27:46 +02:00
parent fda7caef93
commit 90c0791345
11 changed files with 30 additions and 39 deletions

View File

@ -54,10 +54,6 @@ fn tos2(s byteptr) string {
return res return res
} }
fn tos_no_len(s byteptr) string {
return tos2(s)
}
fn (a string) clone() string { fn (a string) clone() string {
mut b := string { mut b := string {
len: a.len len: a.len

View File

@ -160,7 +160,6 @@ fn (p mut Parser) fn_decl() {
} }
if p.tok == PLUS || p.tok == MINUS || p.tok == MUL { if p.tok == PLUS || p.tok == MINUS || p.tok == MUL {
f.name = p.tok.str() f.name = p.tok.str()
println('!!! $f.name')
p.next() p.next()
} }
else { else {

View File

@ -1274,16 +1274,13 @@ fn (p mut Parser) name_expr() string {
if p.table.known_type(name) { if p.table.known_type(name) {
// float(5), byte(0), (*int)(ptr) etc // float(5), byte(0), (*int)(ptr) etc
if p.peek() == LPAR || (deref && p.peek() == RPAR) { if p.peek() == LPAR || (deref && p.peek() == RPAR) {
// println('CASTT $name')
if deref { if deref {
// p.check(RPAR)
// p.next()
name += '*' name += '*'
} }
else if ptr { else if ptr {
name += '*' name += '*'
} }
p.gen('(/*casttt*/') p.gen('(')
mut typ := p.cast(name) mut typ := p.cast(name)
p.gen(')') p.gen(')')
for p.tok == DOT { for p.tok == DOT {
@ -2470,27 +2467,29 @@ fn (p mut Parser) struct_init(is_c_struct_init bool) string {
// `f32(3)` // `f32(3)`
// tok is `f32` or `)` if `(*int)(ptr)` // tok is `f32` or `)` if `(*int)(ptr)`
fn (p mut Parser) cast(typ string) string { fn (p mut Parser) cast(typ string) string {
// typ := p.lit
if p.file_path.contains('test') {
println('CAST TYP=$typ tok=')
p.print_tok()
}
p.gen('($typ)(')
// p.fgen(typ)
p.next() p.next()
pos := p.cgen.add_placeholder()
if p.tok == RPAR { if p.tok == RPAR {
// skip `)` if it's `(*int)(ptr)`, not `int(a)` // skip `)` if it's `(*int)(ptr)`, not `int(a)`
p.ptr_cast = true p.ptr_cast = true
p.next() p.next()
} }
p.check(LPAR) p.check(LPAR)
p.gen('/*77*/')
expr_typ := p.bool_expression() expr_typ := p.bool_expression()
p.check(RPAR) p.check(RPAR)
p.gen(')') // `string(buffer)` => `tos2(buffer)`
if typ == 'string' && expr_typ == 'int' { if typ == 'string' && (expr_typ == 'byte*' || expr_typ == 'byteptr') {
p.error('cannot convert `$expr_typ` to `$typ`') p.cgen.set_placeholder(pos, 'tos2(')
} }
// `string(234)` => error
else if typ == 'string' && expr_typ == 'int' {
p.error('cannot cast `$expr_typ` to `$typ`, use `str()` method instead')
}
else {
p.cgen.set_placeholder(pos, '($typ)(')
// p.fgen(typ)
}
p.gen(')')
return typ return typ
} }

View File

@ -144,4 +144,4 @@ fn main() {
} }
} }
game_loop() game_loop()
} }

View File

@ -9,4 +9,4 @@ fn main(){
l.set_level(log.DEBUG) l.set_level(log.DEBUG)
l.debug('debug') l.debug('debug')
l.fatal('fatal') l.fatal('fatal')
} }

View File

@ -86,14 +86,14 @@ pub fn shader_info_log(shader int) string {
# char infoLog[512]; # char infoLog[512];
# glGetShaderInfoLog(shader, 512, NULL, infoLog); # glGetShaderInfoLog(shader, 512, NULL, infoLog);
# printf("log=%s\n", infoLog); # printf("log=%s\n", infoLog);
# return tos_no_len(infoLog); # return tos2(infoLog);
return '' return ''
} }
pub fn get_program_info_log(program int) string { pub fn get_program_info_log(program int) string {
# char infoLog[512]; # char infoLog[512];
# glGetProgramInfoLog(program, 1024, NULL, infoLog); # glGetProgramInfoLog(program, 1024, NULL, infoLog);
# return tos_no_len(infoLog); # return tos2(infoLog);
return '' return ''
} }

View File

@ -255,10 +255,7 @@ pub fn key_pressed(wnd voidptr, key int) bool {
// TODO not mut // TODO not mut
pub fn (w mut Window) get_clipboard_text() string { pub fn (w mut Window) get_clipboard_text() string {
return tos2(C.glfwGetClipboardString(w.data)) return string(byteptr(C.glfwGetClipboardString(w.data)))
// # char *c = glfwGetClipboardString(w->data);
// # return tos_no_len(c);
// return ''
} }
pub fn (w &Window) set_clipboard_text(s string) { pub fn (w &Window) set_clipboard_text(s string) {

View File

@ -64,11 +64,11 @@ fn write_fn(contents byteptr, size, nmemb int, _mem *MemoryStruct) int {
} }
contents += start + 1 contents += start + 1
// printf("GOOD CONTEnTS=%s\n", contents); // printf("GOOD CONTEnTS=%s\n", contents);
s := tos_no_len(contents) s := string(contents)
// mem.ws_func('kek', 0) // mem.ws_func('kek', 0)
# mem->ws_func(s, mem->user_ptr); # mem->ws_func(s, mem->user_ptr);
} }
mut c := tos_no_len(contents) mut c := string(contents)
c = c.trim_space() c = c.trim_space()
// Need to clone because libcurl reuses this memory // Need to clone because libcurl reuses this memory
mem.strings << c.clone() mem.strings << c.clone()
@ -146,7 +146,7 @@ fn (req &Request) do() Response {
err := C.curl_easy_strerror(res) err := C.curl_easy_strerror(res)
println('curl_easy_perform() failed: $err') println('curl_easy_perform() failed: $err')
} }
body := chunk.strings.join('')// tos_no_len(chunk.memory) body := chunk.strings.join('')// string(chunk.memory)
// chunk.strings.free() // chunk.strings.free()
// resp.headers = hchunk.strings // resp.headers = hchunk.strings
if hchunk.strings.len == 0 { if hchunk.strings.len == 0 {
@ -192,11 +192,11 @@ fn (req &Request) do() Response {
} }
fn unescape(s string) string { fn unescape(s string) string {
return tos2(C.curl_unescape(s.cstr(), s.len)) return string(byteptr(C.curl_unescape(s.cstr(), s.len)))
} }
fn escape(s string) string { fn escape(s string) string {
return tos2(C.curl_escape(s.cstr(), s.len)) return string(byteptr(C.curl_escape(s.cstr(), s.len)))
} }
// //////////////// // ////////////////

View File

@ -156,7 +156,7 @@ fn (req &Request) do() Response {
println('ireadfile()') println('ireadfile()')
buf[nr_read] = 0 buf[nr_read] = 0
C.printf('buf="%s"\n', buf) C.printf('buf="%s"\n', buf)
s += tos2(buf)// TODO perf s += string(buf)// TODO perf
nr_read = 0 nr_read = 0
} }
C.InternetCloseHandle(request) C.InternetCloseHandle(request)

View File

@ -50,4 +50,4 @@ pub fn (l Log) debug(s string){
f := termcolor.blue('D') f := termcolor.blue('D')
println('[$f]$s') println('[$f]$s')
} }
} }

View File

@ -27,8 +27,8 @@ import const (
) )
fn C.getline(voidptr, voidptr, voidptr) int fn C.getline(voidptr, voidptr, voidptr) int
fn C.ftell(fp voidptr) int fn C.ftell(fp voidptr) int
fn C.getenv(byteptr) byteptr
fn todo_remove(){} fn todo_remove(){}
@ -45,7 +45,7 @@ fn init_os_args(argc int, _argv *byteptr) []string {
} }
fn parse_windows_cmd_line(cmd byteptr) []string { fn parse_windows_cmd_line(cmd byteptr) []string {
s := tos2(cmd) s := string(cmd)
return s.split(' ') return s.split(' ')
} }
@ -313,7 +313,7 @@ pub fn getenv(key string) string {
if isnil(s) { if isnil(s) {
return '' return ''
} }
return tos2(s) return string(s)
} }
// `file_exists` returns true if `path` exists. // `file_exists` returns true if `path` exists.
@ -473,4 +473,4 @@ fn on_segfault(f voidptr) {
# sa.sa_flags = SA_SIGINFO; # sa.sa_flags = SA_SIGINFO;
# sigaction(SIGSEGV, &sa, 0); # sigaction(SIGSEGV, &sa, 0);
} }
} }