Пример #1
0
 void ifStatmentCompiler(Block block, Dictionary<string, Variable> locals, int level)
 {
     var command = Regex.Match(lines[block.Start - 1].Trim(), @"^if[ ]*\((?<command>[ ]*.*[ ]*)\)[ ]*:").Groups["command"].Value.Trim();
     int ifEndNumber = ifCounter++;
     int ifStartNumber = ifCounter++;
     smallLogicCompiler(command, locals, false, ifStartNumber, ifEndNumber);
     assemblyText.Add("if" + ifStartNumber + ":");
     blockCompilator(block, locals, level, 0);
     assemblyText.Add("if" + ifEndNumber + ":");
 }
Пример #2
0
 void inlineAssembler(Block block, Dictionary<string, Variable> locals, int argsCount)
 {
     currentLine = block.Start;
     for (; currentLine <= block.End; currentLine++)
     {
         String line = lines[currentLine].Trim();
         Match match = Regex.Match(line, @"\^" + variableNameRegex);
         if (match.Success)
         {
             Variable var = locals[match.Groups["name"].Value];
             line = Regex.Replace(line, @"\^" + variableNameRegex, buildStackVariable(locals, var.Name, argsCount));
             line += " #" + var.Name;
         }
         assemblyText.Add(line);
     }
 }
Пример #3
0
        void blockCompilator(Block block, Dictionary<string, Variable> locals, int level, int argsCount)
        {
            List<Variable> localLocals = new List<Variable>();
            currentLine = block.Start;
            for (; currentLine <= block.End; )
            {
                if (Regex.IsMatch(lines[currentLine].Trim(), @"^[\s]*#"))
                {
                    currentLine++;
                    continue;
                }
                Variable var = variableDeclarator(lines[currentLine].Trim());
                if (var != null)
                {
                    var.StackPosition = locals.Count;
                    localLocals.Add(var);
                    locals.Add(var.Name, var);
                    assemblyText.Add("sub $4,%esp #" + var.Name);
                    currentLine++;
                    continue;
                }
                if (Regex.IsMatch(lines[currentLine].Trim(), @"^asm:[\s]*$"))
                {
                    Block newBlock = findBlock(level + 1, currentLine + 1);
                    inlineAssembler(newBlock, locals, argsCount);
                    continue;
                }
                if (isVariableOperiation(lines[currentLine]))
                {
                    variableOperation(lines[currentLine], locals, argsCount);
                    currentLine++;
                    continue;
                }
                if (isFunctionCalling(lines[currentLine].Trim()))
                {
                    functionCalling(lines[currentLine].Trim(), locals, argsCount);
                    currentLine++;
                    continue;
                }
                if (isIfStatment(lines[currentLine]))
                {
                    Block newBlock = findBlock(level + 1, currentLine + 1);
                    ifStatmentCompiler(newBlock, locals, level + 1);
                }
                if (Regex.IsMatch(lines[currentLine].Trim(), @"^ret[ ]+"))
                {
                    variableExtractor(lines[currentLine].Trim().Substring(3), locals);
                    assemblyText.Add("mov %ebx,%eax");
                    assemblyText.Add("add $" + localLocals.Count * 4 + ",%esp");
                    assemblyText.Add("mov %ebp,%esp");
                    assemblyText.Add("pop %ebx");
                    assemblyText.Add("pop %ebp");
                    assemblyText.Add("ret");
                    currentLine++;
                }
                currentLine++;

            }
            assemblyText.Add("add $" + localLocals.Count * 4 + ",%esp");

            foreach (var local in localLocals)
            {
                locals.Remove(local.Name);
            }
        }