diff --git a/HISTORY b/HISTORY index 26c46b7..8091fb3 100755 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,8 @@ Jan. 25 2016 Fixed a memory leak when printing a referenced usertype +Fixed a bug with nested IF statement +Fixed an indexing bug with list +Added a new sample script source file Jan. 23 2016 Added lazy evaluation for ranged list @@ -283,8 +286,8 @@ Improved memory pool Oct. 11 2015 Added a CLONE statement/function for collections -Optimized LIST indexing -Optimized LIST counting +Optimized list indexing +Optimized list counting Oct. 10 2015 Improved stability for nested IF statement diff --git a/core/my_basic.c b/core/my_basic.c index 458caee..6bb639e 100755 --- a/core/my_basic.c +++ b/core/my_basic.c @@ -6108,7 +6108,7 @@ _ls_node_t* _node_at_list(_list_t* coll, int index) { coll->cached_index++; } else if(index < coll->cached_index) { coll->cached_node = coll->cached_node->prev; - coll->cached_node--; + coll->cached_index--; } } result = coll->cached_node; @@ -12082,6 +12082,7 @@ int _core_if(mb_interpreter_t* s, void** l) { _object_t* val = 0; _object_t* obj = 0; bool_t multi_line = false; + bool_t skip = false; _running_context_t* running = 0; mb_assert(s && l); @@ -12101,6 +12102,8 @@ _elseif: obj = (_object_t*)ast->data; if(val->data.integer) { + skip = true; + if(!_IS_FUNC(obj, _core_then)) { _handle_error_on_obj(s, SE_RN_INTEGER_EXPECTED, s->source_file, DON(ast), MB_FUNC_ERR, _exit, result); } @@ -12113,6 +12116,11 @@ _elseif: ast = ast->next; while(ast && _IS_EOS(ast->data)) ast = ast->next; + if(ast && _IS_FUNC(ast->data, _core_endif)) { + ast = ast->prev; + + break; + } result = _execute_statement(s, &ast); if(result != MB_FUNC_OK) goto _exit; @@ -12159,6 +12167,8 @@ _elseif: obj = (_object_t*)ast->data; if(obj->type != _DT_EOS) { + skip = true; + if(!_IS_FUNC(obj, _core_else)) { _handle_error_on_obj(s, SE_RN_ELSE_EXPECTED, s->source_file, DON(ast), MB_FUNC_ERR, _exit, result); } @@ -12167,6 +12177,11 @@ _elseif: ast = ast->next; while(_IS_EOS(ast->data)) ast = ast->next; + if(ast && _IS_FUNC(ast->data, _core_endif)) { + ast = ast->prev; + + break; + } result = _execute_statement(s, &ast); if(result != MB_FUNC_OK) goto _exit; @@ -12184,8 +12199,10 @@ _exit: if(result == MB_SUB_RETURN) { ast = ast->prev; } else { - if(multi_line) - result = _skip_to(s, &ast, _core_endif, _DT_NIL); + if(multi_line) { + if(skip) + result = _skip_struct(s, &ast, _core_if, _core_endif); + } } *l = ast; diff --git a/my_basic.vcproj b/my_basic.vcproj index 917d5f5..b4a7a34 100755 --- a/my_basic.vcproj +++ b/my_basic.vcproj @@ -329,6 +329,26 @@ /> + + + + + + + + "]" + i = i + 1 + wend + if i < l then + i = i + 1 + endif + + return i +enddef + +def backward(cmd, i) + while i > 0 and mid(cmd, i, 1) <> "[" + i = i - 1 + wend + if i > 0 then + i = i - 1 + endif + + return i +enddef + +def brainfuck(cmd) + m = list() + + i = 0 + cursor = 0 + + l = len(cmd) + while i < l + c = mid(cmd, i, 1) + if c = ">" then + cursor = cursor + 1 + elseif c = "<" then + cursor = cursor - 1 + elseif c = "+" then + reserve(m, cursor) + b = m(cursor) + m(cursor) = b + 1 + elseif c = "-" then + reserve(m, cursor) + b = m(cursor) + m(cursor) = b - 1 + elseif c = "." then + reserve(m, cursor) + print chr(m(cursor)) + elseif c = "," then + reserve(m, cursor) + input ch$ + m(cursor) = asc(ch$) + elseif c = "[" then + reserve(m, cursor) + b = m(cursor) + if b = 0 then + i = forward(cmd, i) + endif + elseif c = "]" then + reserve(m, cursor) + b = m(cursor) + if b <> 0 then + i = backward(cmd, i) + endif + endif + i = i + 1 + wend +enddef + +' This is a brainfuck interpreter written with MY-BASIC +' Try this code (without brackets): "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>." +print "BF: " +input cmd$ + +brainfuck(cmd$)