summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeija <norrepli@gmail.com>2012-08-10 10:19:33 +0200
committerJeija <norrepli@gmail.com>2012-08-10 10:19:33 +0200
commit6a524b8cdd45fc73d8aeed66347e57c81621fb78 (patch)
tree92d8bb3410410c66bca8e01f4e28abf48daff58d
parent0633809f7351be571474c7c6923aa98903396103 (diff)
downloadmesecons-6a524b8cdd45fc73d8aeed66347e57c81621fb78.tar
mesecons-6a524b8cdd45fc73d8aeed66347e57c81621fb78.tar.gz
mesecons-6a524b8cdd45fc73d8aeed66347e57c81621fb78.tar.bz2
mesecons-6a524b8cdd45fc73d8aeed66347e57c81621fb78.tar.xz
mesecons-6a524b8cdd45fc73d8aeed66347e57c81621fb78.zip
Add else: if(condition)command()>command(); > is else
-rw-r--r--mesecons_microcontroller/init.lua51
1 files changed, 44 insertions, 7 deletions
diff --git a/mesecons_microcontroller/init.lua b/mesecons_microcontroller/init.lua
index 3f200a1..530a2cf 100644
--- a/mesecons_microcontroller/init.lua
+++ b/mesecons_microcontroller/init.lua
@@ -159,13 +159,18 @@ function parse_yccode(code, pos)
while true do
command, endi = parse_get_command(code, endi)
if command == nil then return nil end
- if command == true then break end
+ if command == true then break end --end of code
if command == "if" then
r, endi = yc_command_if(code, endi, yc_merge_portstates(Lreal, Lvirtual), eeprom)
if r == nil then return nil end
if r == true then -- nothing
elseif r == false then
- endi = yc_skip_to_endif(code, endi)
+ endi_new = yc_skip_to_else (code, endi)
+ if endi_new == nil then --else > not found
+ endi = yc_skip_to_endif(code, endi)
+ else
+ endi = endi_new
+ end
if endi == nil then return nil end
end
else
@@ -197,16 +202,21 @@ function parse_get_command(code, starti)
s = nil
while s ~= "" do
s = string.sub(code, i, i)
+ if s == "(" then
+ return string.sub(code, starti, i-1), i + 1 -- i: ( i+1 after (
+ end
if s == ";" and starti == i then
starti = starti + 1
i = starti
- s = string.sub(code, i, i)
- end
- if s == "(" then
- return string.sub(code, starti, i-1), i + 1 -- i: ( i+1 after (
+ elseif s == ">" then
+ starti = yc_skip_to_endif(code, starti)
+ if starti == nil then return nil end
+ i = starti
+ else
+ i = i + 1
end
- i = i + 1
end
+
if starti == i-1 then
return true, true
end
@@ -251,9 +261,36 @@ end
function yc_skip_to_endif(code, starti)
local i = starti
local s = false
+ local open_ifs = 1
+ while s ~= nil and s~= "" do
+ s = code:sub(i, i)
+ if s == "i" and code:sub(i+1, i+1) == "f" then --if in µCScript
+ open_ifs = open_ifs + 1
+ end
+ if s == ";" then
+ open_ifs = open_ifs - 1
+ end
+ if open_ifs == 0 then
+ return i + 1
+ end
+ i = i + 1
+ end
+ return nil
+end
+
+function yc_skip_to_else(code, starti)
+ local i = starti
+ local s = false
+ local open_ifs = 1
while s ~= nil and s~= "" do
s = code:sub(i, i)
+ if s == "i" and code:sub(i+1, i+1) == "f" then --if in µCScript
+ open_ifs = open_ifs + 1
+ end
if s == ";" then
+ open_ifs = open_ifs - 1
+ end
+ if open_ifs == 1 and s == ">" then
return i + 1
end
i = i + 1