Code generation and output file gen

This commit is contained in:
Clemens-Dautermann 2020-08-19 22:55:28 +02:00
parent 3ea0178678
commit 26d10cfef0
8 changed files with 197 additions and 69 deletions

View file

@ -7,6 +7,9 @@
<e p="Compiler.cs" t="Include" /> <e p="Compiler.cs" t="Include" />
<e p="Compiler.csproj" t="IncludeRecursive" /> <e p="Compiler.csproj" t="IncludeRecursive" />
<e p="Compiler.sln" t="IncludeFlat" /> <e p="Compiler.sln" t="IncludeFlat" />
<e p="Generator" t="Include">
<e p="Generator.cs" t="Include" />
</e>
<e p="Lexer" t="Include"> <e p="Lexer" t="Include">
<e p="Lexer.cs" t="Include" /> <e p="Lexer.cs" t="Include" />
<e p="Pattern.cs" t="Include" /> <e p="Pattern.cs" t="Include" />

View file

@ -20,14 +20,27 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="a54bb6de-191c-4bd1-91ab-3953adfc5dfb" name="Default Changelist" comment=""> <list default="true" id="a54bb6de-191c-4bd1-91ab-3953adfc5dfb" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/Generator/Generator.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/contentModel.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/contentModel.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Compiler.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Compiler.cs" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Compiler.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Compiler.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Parser/Nodes/NodeType.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Parser/Nodes/NodeType.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Parser/Nodes/ReturnNode.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Parser/Nodes/ReturnNode.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Parser/Parser.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Parser/Parser.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../test.s" beforeDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" /> <option name="LAST_RESOLUTION" value="IGNORE" />
</component> </component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="T4 File" />
</list>
</option>
</component>
<component name="Git.Settings"> <component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." /> <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
</component> </component>
@ -80,12 +93,15 @@
<option value="$PROJECT_DIR$/Parser/Exceptions/WrongTypeException.cs" /> <option value="$PROJECT_DIR$/Parser/Exceptions/WrongTypeException.cs" />
<option value="$PROJECT_DIR$/Lexer/Token.cs" /> <option value="$PROJECT_DIR$/Lexer/Token.cs" />
<option value="$PROJECT_DIR$/Parser/Nodes/Node.cs" /> <option value="$PROJECT_DIR$/Parser/Nodes/Node.cs" />
<option value="$PROJECT_DIR$/Parser/Nodes/ReturnNode.cs" />
<option value="$PROJECT_DIR$/Lexer/Lexer.cs" /> <option value="$PROJECT_DIR$/Lexer/Lexer.cs" />
<option value="$PROJECT_DIR$/Parser/Exceptions/MissingTokenException.cs" /> <option value="$PROJECT_DIR$/Parser/Exceptions/MissingTokenException.cs" />
<option value="$PROJECT_DIR$/Parser/Exceptions/UnexpectedTokenException.cs" /> <option value="$PROJECT_DIR$/Parser/Exceptions/UnexpectedTokenException.cs" />
<option value="$PROJECT_DIR$/Parser/Parser.cs" />
<option value="$PROJECT_DIR$/Lexer/TokenType.cs" /> <option value="$PROJECT_DIR$/Lexer/TokenType.cs" />
<option value="$PROJECT_DIR$/t.tt" />
<option value="$PROJECT_DIR$/Parser/Nodes/NodeType.cs" />
<option value="$PROJECT_DIR$/Parser/Nodes/ReturnNode.cs" />
<option value="$PROJECT_DIR$/Parser/Parser.cs" />
<option value="$PROJECT_DIR$/Generator/Generator.cs" />
<option value="$PROJECT_DIR$/Compiler.cs" /> <option value="$PROJECT_DIR$/Compiler.cs" />
</list> </list>
</option> </option>
@ -148,6 +164,7 @@
<workItem from="1597620399423" duration="389000" /> <workItem from="1597620399423" duration="389000" />
<workItem from="1597650936637" duration="537000" /> <workItem from="1597650936637" duration="537000" />
<workItem from="1597695400525" duration="4873000" /> <workItem from="1597695400525" duration="4873000" />
<workItem from="1597863017034" duration="7430000" />
</task> </task>
<servers /> <servers />
</component> </component>
@ -177,98 +194,102 @@
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state x="2029" y="149" width="738" height="749" key="#Compiler/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597507583637" /> <state x="2029" y="149" width="738" height="749" key="#Compiler/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597507583637" />
<state x="2339" y="185" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1597867717136">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state x="2339" y="185" key="#com.intellij.execution.impl.EditConfigurationsDialog/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597867717136" />
<state x="2169" y="170" width="430" height="508" key="FileChooserDialogImpl" timestamp="1597507670532"> <state x="2169" y="170" width="430" height="508" key="FileChooserDialogImpl" timestamp="1597507670532">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state x="2169" y="170" width="430" height="508" key="FileChooserDialogImpl/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597507670532" /> <state x="2169" y="170" width="430" height="508" key="FileChooserDialogImpl/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597507670532" />
<state width="1868" height="383" key="GridCell.Tab.0.bottom" timestamp="1597700274767"> <state width="1868" height="577" key="GridCell.Tab.0.bottom" timestamp="1597868778558">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.0.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="577" key="GridCell.Tab.0.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597868778558" />
<state width="1868" height="383" key="GridCell.Tab.0.center" timestamp="1597700274767"> <state width="1868" height="577" key="GridCell.Tab.0.center" timestamp="1597868778558">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.0.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="577" key="GridCell.Tab.0.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597868778558" />
<state width="1868" height="383" key="GridCell.Tab.0.left" timestamp="1597700274767"> <state width="1868" height="577" key="GridCell.Tab.0.left" timestamp="1597868778558">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.0.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="577" key="GridCell.Tab.0.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597868778558" />
<state width="1868" height="383" key="GridCell.Tab.0.right" timestamp="1597700274767"> <state width="1868" height="577" key="GridCell.Tab.0.right" timestamp="1597868778558">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.0.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="577" key="GridCell.Tab.0.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597868778558" />
<state width="1868" height="383" key="GridCell.Tab.1.bottom" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.1.bottom" timestamp="1597865244687">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.1.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.1.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244687" />
<state width="1868" height="383" key="GridCell.Tab.1.center" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.1.center" timestamp="1597865244687">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.1.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.1.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244687" />
<state width="1868" height="383" key="GridCell.Tab.1.left" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.1.left" timestamp="1597865244687">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.1.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.1.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244687" />
<state width="1868" height="383" key="GridCell.Tab.1.right" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.1.right" timestamp="1597865244687">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.1.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.1.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244687" />
<state width="1868" height="383" key="GridCell.Tab.2.bottom" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.2.bottom" timestamp="1597865244687">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.2.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.2.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244687" />
<state width="1868" height="383" key="GridCell.Tab.2.center" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.2.center" timestamp="1597865244687">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.2.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.2.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244687" />
<state width="1868" height="383" key="GridCell.Tab.2.left" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.2.left" timestamp="1597865244687">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.2.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.2.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244687" />
<state width="1868" height="383" key="GridCell.Tab.2.right" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.2.right" timestamp="1597865244687">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.2.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.2.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244687" />
<state width="1868" height="383" key="GridCell.Tab.3.bottom" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.3.bottom" timestamp="1597865244687">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.3.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.3.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244687" />
<state width="1868" height="383" key="GridCell.Tab.3.center" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.3.center" timestamp="1597865244687">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.3.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.3.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244687" />
<state width="1868" height="383" key="GridCell.Tab.3.left" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.3.left" timestamp="1597865244687">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.3.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.3.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244687" />
<state width="1868" height="383" key="GridCell.Tab.3.right" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.3.right" timestamp="1597865244687">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.3.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.3.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244687" />
<state width="1868" height="383" key="GridCell.Tab.4.bottom" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.4.bottom" timestamp="1597865244688">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.4.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.4.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244688" />
<state width="1868" height="383" key="GridCell.Tab.4.center" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.4.center" timestamp="1597865244688">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.4.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.4.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244688" />
<state width="1868" height="383" key="GridCell.Tab.4.left" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.4.left" timestamp="1597865244687">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.4.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.4.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244687" />
<state width="1868" height="383" key="GridCell.Tab.4.right" timestamp="1597700274767"> <state width="1868" height="383" key="GridCell.Tab.4.right" timestamp="1597865244688">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state width="1868" height="383" key="GridCell.Tab.4.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597700274767" /> <state width="1868" height="383" key="GridCell.Tab.4.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597865244688" />
<state x="1989" y="220" width="806" height="524" key="Rider.ProjectTemplateDialog.Size" timestamp="1597507532464"> <state x="1989" y="220" width="806" height="524" key="Rider.ProjectTemplateDialog.Size" timestamp="1597507532464">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state x="1989" y="220" width="806" height="524" key="Rider.ProjectTemplateDialog.Size/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597507532464" /> <state x="1989" y="220" width="806" height="524" key="Rider.ProjectTemplateDialog.Size/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597507532464" />
<state x="2342" y="83" width="709" height="485" key="RiderGenerateDialog" timestamp="1597696849168"> <state x="2336" y="41" width="709" height="485" key="RiderGenerateDialog" timestamp="1597863779733">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>
<state x="2342" y="83" width="709" height="485" key="RiderGenerateDialog/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597696849168" /> <state x="2336" y="41" width="709" height="485" key="RiderGenerateDialog/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597863779733" />
<state x="2233" y="185" width="1031" height="724" key="SettingsEditor" timestamp="1597620460972"> <state x="2233" y="185" width="1031" height="724" key="SettingsEditor" timestamp="1597620460972">
<screen x="1920" y="0" width="1920" height="1080" /> <screen x="1920" y="0" width="1920" height="1080" />
</state> </state>

View file

@ -1,30 +1,75 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading;
using Compiler.Lexer; using Compiler.Lexer;
using Compiler.Parser;
using Compiler.Parser.Nodes; using Compiler.Parser.Nodes;
namespace Compiler namespace Compiler
{ {
public static class Compiler public static class Compiler
{ {
public static void Main() public static void Main(string[] args)
{ {
string[] validFiles = Directory.GetFiles("../../../../tests/week_1/valid"); if (args.Length != 1)
string[] invalidFiles = Directory.GetFiles("../../../../tests/week_1/invalid"); {
Console.WriteLine("Usage: Compiler <input path>");
}
else
{
string outputPath = args[0].Substring(0, args[0].LastIndexOf("/"));
foreach (string filepath in validFiles) Compile(args[0], $"{outputPath}/assembly.s");
Console.WriteLine($"Compiled to {outputPath}/assembly.s");
ProcessStartInfo startInfo = new ProcessStartInfo()
{FileName = "gcc", Arguments = $"{outputPath}/assembly.s -o {outputPath}/program"};
Process proc = new Process() {StartInfo = startInfo,};
proc.Start();
while (!proc.HasExited)
{ {
List<Token> tokens = TestLexer(filepath, 0); Thread.Sleep(1);
TestParser(tokens, filepath, 1); }
Console.WriteLine($"Assembled to {outputPath}/program");
File.Delete($"{outputPath}/assembly.s");
Console.WriteLine("Deleted assembly.s file. Done!");
}
} }
foreach (string filepath in invalidFiles)
static void Compile(string inputPath, string outputPath)
{ {
List<Token> tokens = TestLexer(filepath, 0); //Lexing
TestParser(tokens, filepath, 1); Lexer.Lexer lexer = new Lexer.Lexer();
StreamReader file = new StreamReader(inputPath);
string contents = file.ReadToEnd();
List<Token> tokens = lexer.Lex(contents);
Console.WriteLine($"Lexed {inputPath.Split("/").Last()}.");
//Parsing
Parser.Parser p = new Parser.Parser(tokens);
try
{
Node programNode = p.Parse(NodeType.ProgramNode);
Console.WriteLine($"Parsed \"{inputPath.Split("/").Last()}\"");
//Generating
Generator.Generator generator = new Generator.Generator();
string program = generator.Generate(programNode);
File.WriteAllText(outputPath, program);
}
catch (Exception e)
{
Console.WriteLine($"Error in file \"{inputPath.Split("/").Last()}\"");
Console.WriteLine(e.Message);
} }
} }
@ -37,6 +82,20 @@ namespace Compiler
} }
} }
static void TestGenerator(Node root, string destinationPath, int debugLevel)
{
if (root != null)
{
Generator.Generator gen = new Generator.Generator();
string asm = gen.Generate(root);
if (debugLevel > 0)
{
Console.Write(asm);
}
}
}
static List<Token> TestLexer(string path, int debugLevel) static List<Token> TestLexer(string path, int debugLevel)
{ {
Lexer.Lexer lexer = new Lexer.Lexer(); Lexer.Lexer lexer = new Lexer.Lexer();
@ -50,20 +109,17 @@ namespace Compiler
if (debugLevel > 0) if (debugLevel > 0)
{ {
Console.WriteLine("-----------" + path + "-----------");
foreach (Token token in tokens) foreach (Token token in tokens)
{ {
Console.WriteLine(token.ToString()); Console.WriteLine(token.ToString());
} }
Console.WriteLine("--------------------------------------");
} }
return tokens; return tokens;
} }
static void TestParser(List<Token> tokenList, string path, int debugLevel) static Node TestParser(List<Token> tokenList, string path, int debugLevel)
{ {
Parser.Parser p = new Parser.Parser(tokenList); Parser.Parser p = new Parser.Parser(tokenList);
@ -75,12 +131,16 @@ namespace Compiler
{ {
PrettyPrint(programNode, ""); PrettyPrint(programNode, "");
} }
return programNode;
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("Error in file \"" + path.Split("/").Last() + "\""); Console.WriteLine("Error in file \"" + path.Split("/").Last() + "\"");
Console.WriteLine(e.Message); Console.WriteLine(e.Message);
} }
return null;
} }
} }
} }

View file

@ -0,0 +1,48 @@
using System;
using System.Linq;
using Compiler.Parser;
using Compiler.Parser.Nodes;
namespace Compiler.Generator
{
public class Generator
{
public string Generate(Node rootNode)
{
string s = "";
switch (rootNode.NodeType)
{
case NodeType.ProgramNode:
foreach (Node rootNodeChild in rootNode.Children)
{
s += Generate(rootNodeChild);
}
break;
case NodeType.FunctionNode:
string identifier = ((FunctionNode) rootNode).Name;
string functionBody = "";
foreach (Node rootNodeChild in rootNode.Children)
{
functionBody += Generate(rootNodeChild);
}
s = $".globl {identifier}\n" +
$"{identifier}:\n" +
$"{functionBody}";
break;
case NodeType.ReturnStatementNode:
s = $"movl ${Generate(rootNode.Children[0])}, %eax \n" +
"ret\n";
break;
case NodeType.ExpressionNode:
s = ((ConstantNode) rootNode).value.ToString();
break;
default:
throw new ArgumentOutOfRangeException();
}
return s;
}
}
}

View file

@ -1,10 +1,10 @@
namespace Compiler.Parser namespace Compiler.Parser.Nodes
{ {
public enum NodeType public enum NodeType
{ {
ProgramNode, ProgramNode,
FunctionNode, FunctionNode,
StatementNode, ReturnStatementNode,
ExpressionNode ExpressionNode
} }
} }

View file

@ -6,7 +6,7 @@ namespace Compiler.Parser.Nodes
public ReturnNode() public ReturnNode()
{ {
this.NodeType = NodeType.StatementNode; this.NodeType = NodeType.ReturnStatementNode;
} }
} }
} }

View file

@ -80,14 +80,14 @@ namespace Compiler.Parser
CheckFirstTokenAndRemove(TokenType.OpenBraceToken); CheckFirstTokenAndRemove(TokenType.OpenBraceToken);
//add returned child node to AST //add returned child node to AST
n.Children.Add(Parse(NodeType.StatementNode)); n.Children.Add(Parse(NodeType.ReturnStatementNode));
//remove trailing } //remove trailing }
CheckFirstTokenAndRemove(TokenType.CloseBraceToken); CheckFirstTokenAndRemove(TokenType.CloseBraceToken);
break; break;
case NodeType.StatementNode: case NodeType.ReturnStatementNode:
//TODO: This Type of return/statement node will probably need fixing later //TODO: This Type of return/statement node will probably need fixing later
n = new ReturnNode(); n = new ReturnNode();

4
test.s
View file

@ -1,4 +0,0 @@
.globl main
main:
movl $15, %eax
ret