Skip to content

Commit

Permalink
Replace all remaining C code with V in the compiler and vlib (hoorah!)
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Jun 27, 2019
1 parent 554f083 commit 6824e6e
Show file tree
Hide file tree
Showing 19 changed files with 369 additions and 663 deletions.
7 changes: 3 additions & 4 deletions builtin/builtin.v
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub fn print(s string) {
C.printf('%.*s', s.len, s.str)
}

__global total_m i64 = 0
pub fn malloc(n int) byteptr {
if n < 0 {
panic('malloc(<0)')
Expand All @@ -67,10 +68,8 @@ pub fn malloc(n int) byteptr {
}
#endif
#ifdef DEBUG_ALLOC
total := i64(0)
# total_m += n;
# total = total_m;
println('\n\n\nmalloc($n) total=$total')
total_m += n
println('\n\n\nmalloc($n) total=$total_m')
print_backtrace()
#endif
ptr := C.malloc(n)
Expand Down
21 changes: 12 additions & 9 deletions builtin/string.v
Original file line number Diff line number Diff line change
Expand Up @@ -496,15 +496,17 @@ pub fn (ar[]int) contains(val int) bool {
return false
}

/*
pub fn (a[]string) to_c() voidptr {
# char ** res = malloc(sizeof(char*) * a.len);
char ** res = malloc(sizeof(char*) * a.len);
for i := 0; i < a.len; i++ {
val := a[i]
# res[i] = val.str;
}
# return res;
return res;
return 0
}
*/

fn is_space(c byte) bool {
return C.isspace(c)
Expand Down Expand Up @@ -619,8 +621,8 @@ pub fn (s string) ustring() ustring {
runes: new_array(0, s.len, sizeof(int))
}
for i := 0; i < s.len; i++ {
char_len := 0
# char_len =UTF8_CHAR_LEN(s.str[i]);
char_len := utf8_char_len(s.str[i])
//# char_len =UTF8_CHAR_LEN(s.str[i]);
// println('cl=$char_len')
res.runes << i
i += char_len - 1
Expand All @@ -632,18 +634,19 @@ pub fn (s string) ustring() ustring {
// A hack that allows to create ustring without allocations.
// It's called from functions like draw_text() where we know that the string is going to be freed
// right away. Uses global buffer for storing runes []int array.
# array_int g_ustring_runes;
__global g_ustring_runes []int
pub fn (s string) ustring_tmp() ustring {
mut res := ustring {
s: s
runes: 0
}
# res.runes = g_ustring_runes ;
# res.runes.len = s.len ;
res.runes = g_ustring_runes
res.runes.len = s.len
mut j := 0
for i := 0; i < s.len; i++ {
char_len := 0
# char_len =UTF8_CHAR_LEN(s.str[i]);
//char_len := 0
//# char_len =UTF8_CHAR_LEN(s.str[i]);
char_len := utf8_char_len(s.str[i])
res.runes[j] = i
j++
i += char_len - 1
Expand Down
244 changes: 2 additions & 242 deletions builtin/utf8.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,250 +4,10 @@

module builtin

pub fn (s string) is_utf8() int {
faulty_bytes := 0
len := s.len
i := 0
// # size_t i = 0;
# byte * str = s.str;
#
# while (i < len) {
# if (str[i] <= 0x7F) /* 00..7F */ {
# i += 1;
# }
#else if (str[i] >= 0xC2 && str[i] <= 0xDF) /* C2..DF 80..BF */ {
# if (i + 1 < len) /* Expect a 2nd byte */ {
# if (str[i + 1] < 0x80 || str[i + 1] > 0xBF) {
# printf( "After a first byte between C2 and DF, expecting a 2nd byte between 80 and BF");
# faulty_bytes = 2;
# goto end;
# }
# }
#else {
# printf( "After a first byte between C2 and DF, expecting a 2nd byte.");
# faulty_bytes = 1;
# goto end;
# }
# i += 2;
# }
#else if (str[i] == 0xE0) /* E0 A0..BF 80..BF */ {
# if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
# if (str[i + 1] < 0xA0 || str[i + 1] > 0xBF) {
# printf( "After a first byte of E0, expecting a 2nd byte between A0 and BF.");
# faulty_bytes = 2;
# goto end;
# }
# if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
# printf( "After a first byte of E0, expecting a 3nd byte between 80 and BF.");
# faulty_bytes = 3;
# goto end;
# }
# }
#else {
# printf( "After a first byte of E0, expecting two following bytes.");
# faulty_bytes = 1;
# goto end;
# }
# i += 3;
# }
#else if (str[i] >= 0xE1 && str[i] <= 0xEC) /* E1..EC 80..BF 80..BF */ {
# if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
# if (str[i + 1] < 0x80 || str[i + 1] > 0xBF) {
# printf( "After a first byte between E1 and EC, expecting the 2nd byte between 80 and BF.");
# faulty_bytes = 2;
# goto end;
# }
# if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
# printf( "After a first byte between E1 and EC, expecting the 3rd byte between 80 and BF.");
# faulty_bytes = 3;
# goto end;
# }
# }
#else {
# printf( "After a first byte between E1 and EC, expecting two following bytes.");
# faulty_bytes = 1;
# goto end;
# }
# i += 3;
# }
#else if (str[i] == 0xED) /* ED 80..9F 80..BF */ {
# if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
# if (str[i + 1] < 0x80 || str[i + 1] > 0x9F) {
# printf( "After a first byte of ED, expecting 2nd byte between 80 and 9F.");
# faulty_bytes = 2;
# goto end;
# }
# if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
# printf( "After a first byte of ED, expecting 3rd byte between 80 and BF.");
# faulty_bytes = 3;
# goto end;
# }
# }
#else {
# printf( "After a first byte of ED, expecting two following bytes.");
# faulty_bytes = 1;
# goto end;
# }
# i += 3;
# }
#else if (str[i] >= 0xEE && str[i] <= 0xEF) /* EE..EF 80..BF 80..BF */ {
# if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
# if (str[i + 1] < 0x80 || str[i + 1] > 0xBF) {
# printf( "After a first byte between EE and EF, expecting 2nd byte between 80 and BF.");
# faulty_bytes = 2;
# goto end;
# }
# if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
# printf( "After a first byte between EE and EF, expecting 3rd byte between 80 and BF.");
# faulty_bytes = 3;
# goto end;
# }
# }
#else {
# printf( "After a first byte between EE and EF, two following bytes.");
# faulty_bytes = 1;
# goto end;
# }
# i += 3;
# }
#else if (str[i] == 0xF0) /* F0 90..BF 80..BF 80..BF */ {
# if (i + 3 < len) /* Expect a 2nd, 3rd 3th byte */ {
# if (str[i + 1] < 0x90 || str[i + 1] > 0xBF) {
# printf( "After a first byte of F0, expecting 2nd byte between 90 and BF.");
# faulty_bytes = 2;
# goto end;
# }
# if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
# printf( "After a first byte of F0, expecting 3rd byte between 80 and BF.");
# faulty_bytes = 3;
# goto end;
# }
# if (str[i + 3] < 0x80 || str[i + 3] > 0xBF) {
# printf( "After a first byte of F0, expecting 4th byte between 80 and BF.");
# faulty_bytes = 4;
# goto end;
# }
# }
#else {
# printf( "After a first byte of F0, expecting three following bytes.");
# faulty_bytes = 1;
# goto end;
# }
# i += 4;
# }
#else if (str[i] >= 0xF1 && str[i] <= 0xF3) /* F1..F3 80..BF 80..BF 80..BF */ {
# if (i + 3 < len) /* Expect a 2nd, 3rd 3th byte */ {
# if (str[i + 1] < 0x80 || str[i + 1] > 0xBF) {
# printf( "After a first byte of F1, F2, or F3, expecting a 2nd byte between 80 and BF.");
# faulty_bytes = 2;
# goto end;
# }
# if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
# printf( "After a first byte of F1, F2, or F3, expecting a 3rd byte between 80 and BF.");
# faulty_bytes = 3;
# goto end;
# }
# if (str[i + 3] < 0x80 || str[i + 3] > 0xBF) {
# printf( "After a first byte of F1, F2, or F3, expecting a 4th byte between 80 and BF.");
# faulty_bytes = 4;
# goto end;
# }
# }
#else {
# printf( "After a first byte of F1, F2, or F3, expecting three following bytes.");
# faulty_bytes = 1;
# goto end;
# }
# i += 4;
# }
#else if (str[i] == 0xF4) /* F4 80..8F 80..BF 80..BF */ {
# if (i + 3 < len) /* Expect a 2nd, 3rd 3th byte */ {
# if (str[i + 1] < 0x80 || str[i + 1] > 0x8F) {
# printf( "After a first byte of F4, expecting 2nd byte between 80 and 8F.");
# faulty_bytes = 2;
# goto end;
# }
# if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
# printf( "After a first byte of F4, expecting 3rd byte between 80 and BF.");
# faulty_bytes = 3;
# goto end;
# }
# if (str[i + 3] < 0x80 || str[i + 3] > 0xBF) {
# printf( "After a first byte of F4, expecting 4th byte between 80 and BF.");
# faulty_bytes = 4;
# goto end;
# }
# }
#else {
# printf( "After a first byte of F4, expecting three following bytes.");
# faulty_bytes = 1;
# goto end;
# }
# i += 4;
# }
#else {
# printf( "i=%d Expecting bytes in the following ranges: 00..7F C2..F4.",
# i);
# faulty_bytes = 1;
# goto end;
# }
# }
#
# end: ;
// println('faulty bytes=$faulty_bytes i=$i')
// # printf("c='%c'\n", str[i]);
ok := faulty_bytes == 0
if ok {
return -1
}
if !ok {
println('utf is bad dalen=$len KEK $s sdf')
// s = s.left(i)
}
return i
// return ok
pub fn utf8_char_len(b byte) int {
return (( 0xe5000000 >> (( b >> 3 ) & 0x1e )) & 3 ) + 1
}

/*
fn (s string) runes() []string {
res2 := []string{}
// res := new_empty_array_with_cap_string(s.len)
res := []string{}
if !s.is_utf8() {
mys := s
println('string.me runes bad utf $mys HAHA')
return res
}
for i := 0; i < s.len; i++ {
char_len := 0
# char_len =UTF8_CHAR_LEN(s.str[i]);
switch char_len {
case 1:
// println('ONE')
res <<(char2string(s[i]))
case 2:
// println('TWO')
rune2 := s.substr(i, i + 2)
res <<(rune2)
i++
case 3:
// println('TWO')
rune3 := s.substr(i, i + 3)
res <<(rune3)
i++
i++
case 4:
// println('TWO')
rune4 := s.substr(i, i + 4)
res <<(rune4)
i++
i++
i++
}
}
return res
}
*/
// Convert utf32 to utf8
// utf32 == Codepoint
pub fn utf32_to_str(code u32) string {
Expand Down
10 changes: 0 additions & 10 deletions compiler/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,6 @@ fn (p mut Parser) fn_call(f Fn, method_ph int, receiver_var, receiver_type strin
// If we have a method placeholder,
// we need to preappend "method(receiver, ...)"
else {
// C only knows about functions "array_get", "array_set" etc
// TODO I don't need this?
// mut cgen_typ := receiver_type.replace('*', '')
// if cgen_typ.starts_with('array_') {
// cgen_typ = 'array'
// }
// println('METHOD fn_call name=$cgen_name')
// mut method_call := '${cgen_typ}_${cgen_name}('
mut method_call := '${cgen_name}('
receiver := f.args.first()
if receiver.is_mut && !p.expr_var.is_mut {
Expand All @@ -550,10 +542,8 @@ fn (p mut Parser) fn_call(f Fn, method_ph int, receiver_var, receiver_type strin
p.cgen.set_placeholder(method_ph, '$cast $method_call')
}
p.next()
// p.check(LPAR)
p.fn_call_args(f)
p.gen(')')
// p.check(RPAR)
p.calling_c = false
if is_print {
p.cgen.nogen = false
Expand Down
3 changes: 2 additions & 1 deletion compiler/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ void init_consts();')
if c.build_mode == EMBED_VLIB || c.build_mode == DEFAULT_MODE {
// If we declare these for all modes, then when running `v a.v` we'll get
// `/usr/bin/ld: multiple definition of 'total_m'`
cgen.genln('i64 total_m = 0; // For counting total RAM allocated')
// TODO
//cgen.genln('i64 total_m = 0; // For counting total RAM allocated')
cgen.genln('int g_test_ok = 1; ')
if c.table.imports.contains('json') {
cgen.genln('
Expand Down
Loading

0 comments on commit 6824e6e

Please sign in to comment.