*fixed a bug with nested if statement; *fixed an indexing bug with list; +added a new sample script source file.

This commit is contained in:
paladin-t 2016-01-25 16:50:28 +08:00
parent d7424ec5f6
commit 111ab4a9bc
5 changed files with 129 additions and 5 deletions

View File

@ -1,5 +1,8 @@
Jan. 25 2016 Jan. 25 2016
Fixed a memory leak when printing a referenced usertype 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 Jan. 23 2016
Added lazy evaluation for ranged list Added lazy evaluation for ranged list
@ -283,8 +286,8 @@ Improved memory pool
Oct. 11 2015 Oct. 11 2015
Added a CLONE statement/function for collections Added a CLONE statement/function for collections
Optimized LIST indexing Optimized list indexing
Optimized LIST counting Optimized list counting
Oct. 10 2015 Oct. 10 2015
Improved stability for nested IF statement Improved stability for nested IF statement

View File

@ -6108,7 +6108,7 @@ _ls_node_t* _node_at_list(_list_t* coll, int index) {
coll->cached_index++; coll->cached_index++;
} else if(index < coll->cached_index) { } else if(index < coll->cached_index) {
coll->cached_node = coll->cached_node->prev; coll->cached_node = coll->cached_node->prev;
coll->cached_node--; coll->cached_index--;
} }
} }
result = coll->cached_node; result = coll->cached_node;
@ -12082,6 +12082,7 @@ int _core_if(mb_interpreter_t* s, void** l) {
_object_t* val = 0; _object_t* val = 0;
_object_t* obj = 0; _object_t* obj = 0;
bool_t multi_line = false; bool_t multi_line = false;
bool_t skip = false;
_running_context_t* running = 0; _running_context_t* running = 0;
mb_assert(s && l); mb_assert(s && l);
@ -12101,6 +12102,8 @@ _elseif:
obj = (_object_t*)ast->data; obj = (_object_t*)ast->data;
if(val->data.integer) { if(val->data.integer) {
skip = true;
if(!_IS_FUNC(obj, _core_then)) { 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); _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; ast = ast->next;
while(ast && _IS_EOS(ast->data)) while(ast && _IS_EOS(ast->data))
ast = ast->next; ast = ast->next;
if(ast && _IS_FUNC(ast->data, _core_endif)) {
ast = ast->prev;
break;
}
result = _execute_statement(s, &ast); result = _execute_statement(s, &ast);
if(result != MB_FUNC_OK) if(result != MB_FUNC_OK)
goto _exit; goto _exit;
@ -12159,6 +12167,8 @@ _elseif:
obj = (_object_t*)ast->data; obj = (_object_t*)ast->data;
if(obj->type != _DT_EOS) { if(obj->type != _DT_EOS) {
skip = true;
if(!_IS_FUNC(obj, _core_else)) { 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); _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; ast = ast->next;
while(_IS_EOS(ast->data)) while(_IS_EOS(ast->data))
ast = ast->next; ast = ast->next;
if(ast && _IS_FUNC(ast->data, _core_endif)) {
ast = ast->prev;
break;
}
result = _execute_statement(s, &ast); result = _execute_statement(s, &ast);
if(result != MB_FUNC_OK) if(result != MB_FUNC_OK)
goto _exit; goto _exit;
@ -12184,8 +12199,10 @@ _exit:
if(result == MB_SUB_RETURN) { if(result == MB_SUB_RETURN) {
ast = ast->prev; ast = ast->prev;
} else { } else {
if(multi_line) if(multi_line) {
result = _skip_to(s, &ast, _core_endif, _DT_NIL); if(skip)
result = _skip_struct(s, &ast, _core_if, _core_endif);
}
} }
*l = ast; *l = ast;

View File

@ -329,6 +329,26 @@
/> />
</FileConfiguration> </FileConfiguration>
</File> </File>
<File
RelativePath=".\sample\sample07.bas"
>
<FileConfiguration
Name="debug|Win32"
ExcludedFromBuild="true"
>
<Tool
Name="VCCustomBuildTool"
/>
</FileConfiguration>
<FileConfiguration
Name="release|Win32"
ExcludedFromBuild="true"
>
<Tool
Name="VCCustomBuildTool"
/>
</FileConfiguration>
</File>
</Filter> </Filter>
<Filter <Filter
Name="res" Name="res"

Binary file not shown.

84
sample/sample07.bas Normal file
View File

@ -0,0 +1,84 @@
' This script is an example of MY-BASIC
' Copyright (c) 2011 - 2016 Wang Renxin. All rights reserved.
' For more information, see https://github.com/paladin-t/my_basic/
def reserve(m, n)
for j = len(m) to n + 2
push(m, 0)
next
enddef
def forward(cmd, i)
l = len(cmd)
while i < l and mid(cmd, i, 1) <> "]"
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$)