Implemented lexer

This commit is contained in:
Clemens-Dautermann 2020-08-15 23:36:29 +02:00
parent cb809bfd29
commit ea26acce4a
34 changed files with 641 additions and 0 deletions

3
.gitignore vendored
View file

@ -1 +1,4 @@
venv/*
Compiler/bin/*
Compiler/obj/*
.idea/*

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RIDER_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

7
.idea/.idea.lpc.dir/riderModule.iml generated Normal file
View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RIDER_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$/../.." />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

10
.idea/lpc.iml generated Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.8 (lpc)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RIDER_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ContentModelStore">
<e p="$USER_HOME$/.cache/JetBrains/Rider2020.1/extResources" t="IncludeRecursive" />
<e p="$USER_HOME$/.cache/JetBrains/Rider2020.1/resharper-host/local/Transient/ReSharperHost/v201/SolutionCaches/_Compiler.1953847606.00" t="ExcludeRecursive" />
<e p="$PROJECT_DIR$" t="IncludeRecursive">
<e p="Compiler.cs" t="Include" />
<e p="Compiler.csproj" t="IncludeRecursive" />
<e p="Compiler.sln" t="IncludeFlat" />
<e p="Lexer.cs" t="Include" />
<e p="Pattern.cs" t="Include" />
<e p="Token.cs" t="Include" />
<e p="TokenType.cs" t="Include" />
<e p="bin" t="ExcludeRecursive" />
<e p="obj" t="ExcludeRecursive">
<e p="Debug" t="Include">
<e p="netcoreapp3.1" t="Include">
<e p="Compiler.AssemblyInfo.cs" t="Include" />
</e>
</e>
</e>
<e p="packages" t="ExcludeRecursive" />
</e>
</component>
</project>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ContentModelUserStore">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/.idea.Compiler/.idea/.idea.Compiler.iml" filepath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/.idea.Compiler.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/.idea.Compiler/riderModule.iml" filepath="$PROJECT_DIR$/.idea/.idea.Compiler/riderModule.iml" />
</modules>
</component>
</project>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RiderProjectSettingsUpdater">
<option name="vcsConfiguration" value="2" />
</component>
</project>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View file

@ -0,0 +1,264 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="BranchesTreeState">
<expand>
<path>
<item name="ROOT" type="e8cecc67:BranchNodeDescriptor" />
<item name="LOCAL_ROOT" type="e8cecc67:BranchNodeDescriptor" />
</path>
<path>
<item name="ROOT" type="e8cecc67:BranchNodeDescriptor" />
<item name="REMOTE_ROOT" type="e8cecc67:BranchNodeDescriptor" />
</path>
<path>
<item name="ROOT" type="e8cecc67:BranchNodeDescriptor" />
<item name="REMOTE_ROOT" type="e8cecc67:BranchNodeDescriptor" />
<item name="GROUP_NODE:origin" type="e8cecc67:BranchNodeDescriptor" />
</path>
</expand>
<select />
</component>
<component name="ChangeListManager">
<list default="true" id="a54bb6de-191c-4bd1-91ab-3953adfc5dfb" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/../.idea/.idea.lpc.dir/.idea/.idea.lpc.dir.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../.idea/.idea.lpc.dir/riderModule.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../.idea/lpc.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/.idea.Compiler.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/contentModel.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/indexLayout.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/projectSettingsUpdater.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/.idea.Compiler/riderModule.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Compiler.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Lexer.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Pattern.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Token.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/TokenType.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../grammar" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../tests/week_1/valid/multi_digit.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../tests/week_1/valid/newlines.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../tests/week_1/valid/no_newlines.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../tests/week_1/valid/return_0.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../tests/week_1/valid/return_2.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../tests/week_1/valid/spaces.c" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
</component>
<component name="HighlightingSettingsPerFile">
<setting file="file://$PROJECT_DIR$/Lexer.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/TokenType.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Compiler.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Pattern.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Token.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/714153FB-3DE5-4537-8A59-8AE8F7F7655E/44/126d3750/String.cs" root0="SKIP_HIGHLIGHTING" />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/Compiler/Program.cs" />
<option value="$PROJECT_DIR$/Program.cs" />
<option value="$PROJECT_DIR$/Tokens.cs" />
<option value="$PROJECT_DIR$/Pattern.cs" />
<option value="$PROJECT_DIR$/Token.cs" />
<option value="$PROJECT_DIR$/Compiler.cs" />
<option value="$PROJECT_DIR$/TokenType.cs" />
<option value="$PROJECT_DIR$/Lexer.cs" />
</list>
</option>
</component>
<component name="ProjectId" id="1g8sEwibYOXRSHf7VBAgyNtiM21" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="2" id="Add" />
</component>
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="ASKED_ADD_EXTERNAL_FILES" value="true" />
<property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="DebuggerViewTab_PTCS_FirstProportionKey" value="0.350057" />
<property name="DebuggerViewTab_PTCS_LastProportionKey" value="0.299886" />
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="XThreadsFramesViewSplitterKey" value="0.49837133" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/.." />
<property name="settings.editor.selected.configurable" value="editor.preferences.smartKeys_rider" />
</component>
<component name="RunManager">
<configuration name="Compiler" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/bin/Debug/netcoreapp3.1/Compiler.dll" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/bin/Debug/netcoreapp3.1" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/Compiler.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value=".NETCoreApp,Version=v3.1" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="a54bb6de-191c-4bd1-91ab-3953adfc5dfb" name="Default Changelist" comment="" />
<created>1597507445860</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1597507445860</updated>
<workItem from="1597507449039" duration="231000" />
<workItem from="1597508003307" duration="9083000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="2" />
</component>
<component name="UnityProjectConfiguration" hasMinimizedUI="false" />
<component name="UnityUnitTestConfiguration" currentTestLauncher="NUnit" />
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
<option name="oldMeFiltersMigrated" value="true" />
</component>
<component name="VcsManagerConfiguration">
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
<option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" />
</component>
<component name="WindowStateProjectService">
<state x="2029" y="149" width="738" height="749" key="#Compiler" timestamp="1597507583637">
<screen x="1920" y="0" width="1920" height="1080" />
</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="2169" y="170" width="430" height="508" key="FileChooserDialogImpl" timestamp="1597507670532">
<screen x="1920" y="0" width="1920" height="1080" />
</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 width="908" height="289" key="GridCell.Tab.0.bottom" timestamp="1597526505915">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="289" key="GridCell.Tab.0.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597526505915" />
<state width="908" height="289" key="GridCell.Tab.0.center" timestamp="1597526505914">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="289" key="GridCell.Tab.0.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597526505914" />
<state width="908" height="289" key="GridCell.Tab.0.left" timestamp="1597526505914">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="289" key="GridCell.Tab.0.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597526505914" />
<state width="908" height="289" key="GridCell.Tab.0.right" timestamp="1597526505914">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="289" key="GridCell.Tab.0.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597526505914" />
<state width="908" height="383" key="GridCell.Tab.1.bottom" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.1.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.1.center" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.1.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.1.left" timestamp="1597525905669">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.1.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905669" />
<state width="908" height="383" key="GridCell.Tab.1.right" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.1.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.2.bottom" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.2.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.2.center" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.2.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.2.left" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.2.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.2.right" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.2.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.3.bottom" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.3.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.3.center" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.3.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.3.left" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.3.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.3.right" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.3.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.4.bottom" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.4.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.4.center" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.4.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.4.left" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.4.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state width="908" height="383" key="GridCell.Tab.4.right" timestamp="1597525905670">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="908" height="383" key="GridCell.Tab.4.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525905670" />
<state x="1989" y="220" width="806" height="524" key="Rider.ProjectTemplateDialog.Size" timestamp="1597507532464">
<screen x="1920" y="0" width="1920" height="1080" />
</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="2161" y="167" width="474" height="713" key="RiderGenerateDialog" timestamp="1597510130230">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state x="2161" y="167" width="474" height="713" key="RiderGenerateDialog/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597510130230" />
<state x="886" y="141" width="1031" height="724" key="SettingsEditor" timestamp="1597520511080">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state x="886" y="141" width="1031" height="724" key="SettingsEditor/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597520511080" />
<state width="480" height="540" key="XDebugger.FullValuePopup" timestamp="1597525606220">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state width="480" height="540" key="XDebugger.FullValuePopup/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597525606220" />
<state x="2182" y="199" width="432" height="650" key="com.intellij.openapi.editor.actions.MultiplePasteAction$ClipboardContentChooser" timestamp="1597510086756">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state x="2182" y="199" width="432" height="650" key="com.intellij.openapi.editor.actions.MultiplePasteAction$ClipboardContentChooser/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597510086756" />
</component>
</project>

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RIDER_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$/../.." />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

36
Compiler/Compiler.cs Normal file
View file

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace Compiler
{
public class Compiler
{
public static void Main()
{
TestCompiler("../../../../tests/week_1/valid");
TestCompiler("../../../../tests/week_1/invalid");
}
static void TestCompiler(String path)
{
String[] files = Directory.GetFiles(path);
Lexer lexer = new Lexer();
foreach (String filename in files)
{
StreamReader file = new StreamReader(filename);
String contents = file.ReadToEnd();
List<Token> tokens = lexer.Lex(contents);
Console.WriteLine("-----------" + filename + "-----------");
foreach (Token token in tokens)
{
Console.WriteLine(token.ToString());
}
Console.WriteLine("--------------------------------------");
}
}
}
}

8
Compiler/Compiler.csproj Normal file
View file

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

16
Compiler/Compiler.sln Normal file
View file

@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Compiler", "Compiler.csproj", "{B8389A68-322F-43FD-9934-8747C0D1D6E5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B8389A68-322F-43FD-9934-8747C0D1D6E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8389A68-322F-43FD-9934-8747C0D1D6E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8389A68-322F-43FD-9934-8747C0D1D6E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8389A68-322F-43FD-9934-8747C0D1D6E5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

98
Compiler/Lexer.cs Normal file
View file

@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Compiler
{
class Lexer
{
//this method transforms a String into a list of tokens
public List<Token> Lex(String inputString)
{
//strip newlines
inputString = inputString.Replace("\n", "");
inputString = inputString.Replace("\r", "");
//initialize Token List
List<Token> tokens = new List<Token>();
//while the string is not empty
while (inputString.Length > 0)
{
//trim whitespace
inputString = inputString.Trim();
//get next token
Token t = Next(inputString);
//append token to list
tokens.Add(t);
//remove token from input string
inputString = inputString.Substring(t.Length);
}
return tokens;
}
//this function returns the next token from a string
private Token Next(String input)
{
//initialize different possible patterns
List<Pattern> patterns = new List<Pattern>();
patterns.Add(new Pattern(@"^\(", TokenType.OpenParenthesisToken));
patterns.Add(new Pattern(@"^\)", TokenType.CloseParenthesisToken));
patterns.Add(new Pattern(@"^int ", TokenType.IntToken));
patterns.Add(new Pattern(@"^}", TokenType.CloseBraceToken));
patterns.Add(new Pattern(@"^{", TokenType.OpenBraceToken));
patterns.Add(new Pattern(@"^return ", TokenType.ReturnToken));
patterns.Add(new Pattern(@"^;", TokenType.SemicolonToken));
patterns.Add(new Pattern(@"^[a-zA-Z]\w*", TokenType.IdentifierToken));
patterns.Add(new Pattern(@"^[0-9]+", TokenType.IntegerLiteralToken));
//try each pattern do determine if it is the one matching at the beginning
//TODO: There sure is room for optimization here
foreach (var pattern in patterns)
{
//get regex from pattern and match
Regex r = pattern.ToRegex();
Match m = r.Match(input);
//check if the match was successful and at the beginning of the string
if (m.Index == 0 && m.Success)
{
//generate new token from match
Token t = new Token(pattern.GetTokenType());
t.Length = m.Length;
t.Value = null;
//switch over token types to assign a value if needed
//the default case "InvalidToken" is here for security and should never happen. If it does,
//something is wrong with the regex
switch (pattern.GetTokenType())
{
case TokenType.IntegerLiteralToken:
t.Value = m.Value;
break;
case TokenType.IdentifierToken:
t.Value = m.Value;
break;
case TokenType.ReturnToken:
case TokenType.CloseParenthesisToken:
case TokenType.OpenParenthesisToken:
case TokenType.OpenBraceToken:
case TokenType.CloseBraceToken:
case TokenType.IntToken:
case TokenType.SemicolonToken:
break;
default:
t.TokenType = TokenType.InvalidToken;
throw new Exception("Match found, but no corresponding token. Check regex!");
}
return t;
}
}
//return the next token
return new Token(TokenType.InvalidToken);
}
}
}

27
Compiler/Pattern.cs Normal file
View file

@ -0,0 +1,27 @@
using System;
using System.Text.RegularExpressions;
namespace Compiler
{
public class Pattern
{
private String pattern;
private TokenType tokenType;
public Pattern(String pattern, TokenType tokenType)
{
this.pattern = pattern;
this.tokenType = tokenType;
}
public Regex ToRegex()
{
return new Regex(this.pattern);
}
public TokenType GetTokenType()
{
return this.tokenType;
}
}
}

28
Compiler/Token.cs Normal file
View file

@ -0,0 +1,28 @@
using System;
namespace Compiler
{
public class Token
{
public TokenType TokenType { get; set; }
public String Value { get; set; }
public int Length { get; set; }
public Token(TokenType pTokenType)
{
this.TokenType = pTokenType;
}
public override string ToString()
{
if (Value == null)
{
return TokenType.ToString();
}
else
{
return TokenType.ToString() + ":" + Value.ToString();
}
}
}
}

19
Compiler/TokenType.cs Normal file
View file

@ -0,0 +1,19 @@
namespace Compiler
{
public enum TokenType
{
OpenParenthesisToken,
CloseParenthesisToken,
IntToken,
OpenBraceToken,
CloseBraceToken,
ReturnToken,
SemicolonToken,
IdentifierToken,
IntegerLiteralToken,
//special Token to represent invalid matches
InvalidToken
}
}

0
grammar Normal file
View file

4
test.s Normal file
View file

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

View file

@ -0,0 +1,3 @@
int main( {
return 0;
}

View file

@ -0,0 +1,3 @@
int main() {
return;
}

View file

@ -0,0 +1,2 @@
int main() {
return 0;

View file

@ -0,0 +1,3 @@
int main() {
return 0
}

View file

@ -0,0 +1,3 @@
int main() {
return0;
}

View file

@ -0,0 +1,3 @@
int main() {
RETURN 0;
}

View file

@ -0,0 +1,3 @@
int main() {
return 100;
}

View file

@ -0,0 +1,10 @@
int
main
(
)
{
return
0
;
}

View file

@ -0,0 +1 @@
int main(){return 0;}

View file

@ -0,0 +1,3 @@
int main() {
return 0;
}

View file

@ -0,0 +1,3 @@
int main() {
return 2;
}

View file

@ -0,0 +1 @@
int main ( ) { return 0 ; }