Ejemplo n.º 1
0
        private string TranspileStatement(string input)
        {
            Match match;

            // CODE LABEL
            // #MYLABEL => MYLABEL:
            if ((match = Regex.Match(input, $"^#({validTokenRegex})$")).Success)
            {
                return($"{match.Groups[1].Value}:");
            }

            // JUMP
            // JUMP #MYLABEL => goto MYLABEL;
            if ((match = Regex.Match(input, $"^JUMP #({validTokenRegex})$")).Success)
            {
                return($"goto {match.Groups[1].Value};");
            }

            // END
            // END => }
            if (input == "END")
            {
                return("}");
            }

            // REPEAT
            // REPEAT 10 => for (int xyz = 0; xyz < 10; xyz++) {
            if ((match = Regex.Match(input, $"^REPEAT ([0-9]+)$")).Success)
            {
                var i = VariableNames.RandomName();
                return($"for (var {i} = 0; {i} < {match.Groups[1].Value}; {i}++){System.Environment.NewLine}{{");
            }

            // FOREACH
            // FOREACH v IN list => foreach (var v in list) {
            if ((match = Regex.Match(input, $"^FOREACH ({validTokenRegex}) IN ({validTokenRegex})$")).Success)
            {
                return($"foreach (var {match.Groups[1].Value} in {match.Groups[2].Value}){System.Environment.NewLine}{{");
            }

            // LOG
            // LOG myVar => data.Logger.Log(myVar);
            if ((match = Regex.Match(input, $"^LOG (.+)$")).Success)
            {
                return($"data.Logger.Log({match.Groups[1].Value});");
            }

            // CLOG
            // CLOG Tomato "hello" => data.Logger.Log("hello", LogColors.Tomato);
            if ((match = Regex.Match(input, $"^CLOG ([A-Za-z]+) (.+)$")).Success)
            {
                return($"data.Logger.Log({match.Groups[2].Value}, LogColors.{match.Groups[1].Value});");
            }

            // WHILE
            // WHILE a < b => while (a < b) {
            if ((match = Regex.Match(input, $"^WHILE (.+)$")).Success)
            {
                var line = match.Groups[1].Value.Trim();
                if (LoliCodeParser.KeyTypes.Any(t => line.StartsWith(t)))
                {
                    var keyType = LineParser.ParseToken(ref line);
                    var key     = LoliCodeParser.ParseKey(ref line, keyType);
                    return($"while ({CSharpWriter.ConvertKey(key)}){System.Environment.NewLine}{{");
                }
                else
                {
                    return($"while ({line}){System.Environment.NewLine}{{");
                }
            }

            // IF
            // IF a < b => if (a < b) {
            if ((match = Regex.Match(input, $"^IF (.+)$")).Success)
            {
                var line = match.Groups[1].Value.Trim();
                if (LoliCodeParser.KeyTypes.Any(t => line.StartsWith(t)))
                {
                    var keyType = LineParser.ParseToken(ref line);
                    var key     = LoliCodeParser.ParseKey(ref line, keyType);
                    return($"if ({CSharpWriter.ConvertKey(key)}){System.Environment.NewLine}{{");
                }
                else
                {
                    return($"if ({line}){System.Environment.NewLine}{{");
                }
            }

            // ELSE
            // ELSE => } else {
            if (input == "ELSE")
            {
                return($"}}{System.Environment.NewLine}else{System.Environment.NewLine}{{");
            }

            // ELSE IF
            // ELSE IF a < b => } else if (a < b) {
            if ((match = Regex.Match(input, $"ELSE IF (.+)$")).Success)
            {
                var line = match.Groups[1].Value.Trim();
                if (LoliCodeParser.KeyTypes.Any(t => line.StartsWith(t)))
                {
                    var keyType = LineParser.ParseToken(ref line);
                    var key     = LoliCodeParser.ParseKey(ref line, keyType);
                    return($"}}{System.Environment.NewLine}else if ({CSharpWriter.ConvertKey(key)}){System.Environment.NewLine}{{");
                }
                else
                {
                    return($"}}{System.Environment.NewLine}else if ({line}){System.Environment.NewLine}{{");
                }
            }

            // TRY
            // TRY => try {
            if (input == "TRY")
            {
                return($"try{System.Environment.NewLine}{{");
            }

            // CATCH
            // CATCH => } catch {
            if (input == "CATCH")
            {
                return($"}}{System.Environment.NewLine}catch{System.Environment.NewLine}{{");
            }

            throw new NotSupportedException();
        }
Ejemplo n.º 2
0
        private string TranspileStatement(string input, List <string> definedVariables)
        {
            Match match;

            // (RESOURCES) TAKEONE
            // TAKEONE FROM "MyResource" => "myString"
            if ((match = Regex.Match(input, "TAKEONE FROM (\"[^\"]+\") => @?\"?([^\"]+)\"?")).Success)
            {
                return($"string {match.Groups[2].Value} = globals.Resources[{match.Groups[1].Value}].TakeOne();");
            }

            // (RESOURCES) TAKE
            // TAKE 5 FROM "MyResource" => "myList"
            if ((match = Regex.Match(input, "TAKE ([0-9]+) FROM (\"[^\"]+\") => @?\"?([^\"]+)\"?")).Success)
            {
                return($"List<string> {match.Groups[3].Value} = globals.Resources[{match.Groups[2].Value}].Take({match.Groups[1].Value});");
            }

            // CODE LABEL
            // #MYLABEL => MYLABEL:
            if ((match = Regex.Match(input, $"^#({validTokenRegex})$")).Success)
            {
                return($"{match.Groups[1].Value}:");
            }

            // JUMP
            // JUMP #MYLABEL => goto MYLABEL;
            if ((match = Regex.Match(input, $"^JUMP #({validTokenRegex})$")).Success)
            {
                return($"goto {match.Groups[1].Value};");
            }

            // END
            // END => }
            if (input == "END")
            {
                return("}");
            }

            // REPEAT
            // REPEAT 10 => for (int xyz = 0; xyz < 10; xyz++) {
            if ((match = Regex.Match(input, $"^REPEAT (.+)$")).Success)
            {
                var i = VariableNames.RandomName();
                return($"for (var {i} = 0; {i} < ({match.Groups[1].Value}).AsInt(); {i}++){System.Environment.NewLine}{{");
            }

            // FOREACH
            // FOREACH v IN list => foreach (var v in list) {
            if ((match = Regex.Match(input, $"^FOREACH ({validTokenRegex}) IN ({validTokenRegex})$")).Success)
            {
                return($"foreach (var {match.Groups[1].Value} in {match.Groups[2].Value}){System.Environment.NewLine}{{");
            }

            // LOG
            // LOG myVar => data.Logger.Log(myVar);
            if ((match = Regex.Match(input, $"^LOG (.+)$")).Success)
            {
                return($"data.Logger.Log({match.Groups[1].Value});");
            }

            // CLOG
            // CLOG Tomato "hello" => data.Logger.Log("hello", LogColors.Tomato);
            if ((match = Regex.Match(input, $"^CLOG ([A-Za-z]+) (.+)$")).Success)
            {
                return($"data.Logger.Log({match.Groups[2].Value}, LogColors.{match.Groups[1].Value});");
            }

            // WHILE
            // WHILE a < b => while (a < b) {
            if ((match = Regex.Match(input, $"^WHILE (.+)$")).Success)
            {
                var line = match.Groups[1].Value.Trim();
                if (LoliCodeParser.keyIdentifiers.Any(t => line.StartsWith(t)))
                {
                    var keyType = LineParser.ParseToken(ref line);
                    var key     = LoliCodeParser.ParseKey(ref line, keyType);
                    return($"while ({CSharpWriter.ConvertKey(key)}){System.Environment.NewLine}{{");
                }
                else
                {
                    return($"while ({line}){System.Environment.NewLine}{{");
                }
            }

            // IF
            // IF a < b => if (a < b) {
            if ((match = Regex.Match(input, $"^IF (.+)$")).Success)
            {
                var line = match.Groups[1].Value.Trim();
                if (LoliCodeParser.keyIdentifiers.Any(t => line.StartsWith(t)))
                {
                    var keyType = LineParser.ParseToken(ref line);
                    var key     = LoliCodeParser.ParseKey(ref line, keyType);
                    return($"if ({CSharpWriter.ConvertKey(key)}){System.Environment.NewLine}{{");
                }
                else
                {
                    return($"if ({line}){System.Environment.NewLine}{{");
                }
            }

            // ELSE
            // ELSE => } else {
            if (input == "ELSE")
            {
                return($"}}{System.Environment.NewLine}else{System.Environment.NewLine}{{");
            }

            // ELSE IF
            // ELSE IF a < b => } else if (a < b) {
            if ((match = Regex.Match(input, $"ELSE IF (.+)$")).Success)
            {
                var line = match.Groups[1].Value.Trim();
                if (LoliCodeParser.keyIdentifiers.Any(t => line.StartsWith(t)))
                {
                    var keyType = LineParser.ParseToken(ref line);
                    var key     = LoliCodeParser.ParseKey(ref line, keyType);
                    return($"}}{System.Environment.NewLine}else if ({CSharpWriter.ConvertKey(key)}){System.Environment.NewLine}{{");
                }
                else
                {
                    return($"}}{System.Environment.NewLine}else if ({line}){System.Environment.NewLine}{{");
                }
            }

            // TRY
            // TRY => try {
            if (input == "TRY")
            {
                return($"try{System.Environment.NewLine}{{");
            }

            // CATCH
            // CATCH => } catch {
            if (input == "CATCH")
            {
                return($"}}{System.Environment.NewLine}catch{System.Environment.NewLine}{{");
            }

            // FINALLY
            // FINALLY => } finally {
            if (input == "FINALLY")
            {
                return($"}}{System.Environment.NewLine}finally{System.Environment.NewLine}{{");
            }

            // LOCK
            // LOCK globals => lock (globals) {
            if ((match = Regex.Match(input, $"^LOCK (.+)$")).Success)
            {
                return($"lock({match.Groups[1].Value}){System.Environment.NewLine}{{");
            }

            // ACQUIRELOCK
            // ACQUIRELOCK globals => await data.AsyncLocker.Acquire(nameof(globals), data.CancellationToken);
            if ((match = Regex.Match(input, $"^ACQUIRELOCK (.+)$")).Success)
            {
                return($"await data.AsyncLocker.Acquire(nameof({match.Groups[1].Value}), data.CancellationToken);");
            }

            // RELEASELOCK
            // RELEASELOCK globals => data.AsyncLocker.Release(nameof(globals));
            if ((match = Regex.Match(input, $"^RELEASELOCK (.+)$")).Success)
            {
                return($"data.AsyncLocker.Release(nameof({match.Groups[1].Value}));");
            }

            // SET VAR
            // SET VAR myString "hello" => string myString = "hello";
            if ((match = Regex.Match(input, $"^SET VAR @?\"?({validTokenRegex})\"? (.+)$")).Success)
            {
                if (definedVariables.Contains(match.Groups[1].Value))
                {
                    return($"{match.Groups[1].Value} = {match.Groups[2].Value};");
                }
                else
                {
                    definedVariables.Add(match.Groups[1].Value);
                    return($"string {match.Groups[1].Value} = {match.Groups[2].Value};");
                }
            }

            // SET CAP
            // SET CAP myCapture "hello" => string myString = "hello"; data.MarkForCapture(nameof(myCapture));
            if ((match = Regex.Match(input, $"^SET CAP @?\"?({validTokenRegex})\"? (.+)$")).Success)
            {
                if (definedVariables.Contains(match.Groups[1].Value))
                {
                    return($"{match.Groups[1].Value} = {match.Groups[2].Value};{System.Environment.NewLine}data.MarkForCapture(nameof({match.Groups[1].Value}));");
                }
                else
                {
                    definedVariables.Add(match.Groups[1].Value);
                    return($"string {match.Groups[1].Value} = {match.Groups[2].Value};{System.Environment.NewLine}data.MarkForCapture(nameof({match.Groups[1].Value}));");
                }
            }

            throw new NotSupportedException();
        }