From 6a524b8cdd45fc73d8aeed66347e57c81621fb78 Mon Sep 17 00:00:00 2001 From: Jeija Date: Fri, 10 Aug 2012 10:19:33 +0200 Subject: [PATCH] Add else: if(condition)command()>command(); > is else --- mesecons_microcontroller/init.lua | 53 ++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 8 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 == ";" 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 ( end - i = i + 1 + if s == ";" and starti == i then + starti = starti + 1 + i = starti + elseif s == ">" then + starti = yc_skip_to_endif(code, starti) + if starti == nil then return nil end + i = starti + else + i = i + 1 + end 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