Added Parser

- can parse a basic token list to an AST
This commit is contained in:
Clemens-Dautermann 2020-08-17 01:07:59 +02:00
parent ea26acce4a
commit b3fe78fffb
19 changed files with 451 additions and 88 deletions

View file

@ -7,10 +7,29 @@
<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="Lexer" t="Include">
<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>
<e p="Parser" t="Include">
<e p="Exceptions" t="Include">
<e p="InvalidIdentifierException.cs" t="Include" />
<e p="MissingSemicolonException.cs" t="Include" />
<e p="UnexpectedTokenException.cs" t="Include" />
<e p="WrongTypeException.cs" t="Include" />
</e>
<e p="Nodes" t="Include">
<e p="ConstantNode.cs" t="Include" />
<e p="FunctionNode.cs" t="Include" />
<e p="Node.cs" t="Include" />
<e p="NodeType.cs" t="Include" />
<e p="ProgramNode.cs" t="Include" />
<e p="ReturnNode.cs" t="Include" />
</e>
<e p="Parser.cs" t="Include" />
</e>
<e p="bin" t="ExcludeRecursive" />
<e p="obj" t="ExcludeRecursive">
<e p="Debug" t="Include">

View file

@ -20,29 +20,25 @@
</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" />
<change afterPath="$PROJECT_DIR$/Parser/Exceptions/InvalidIdentifierException.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Parser/Exceptions/MissingSemicolonException.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Parser/Exceptions/UnexpectedTokenException.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Parser/Exceptions/WrongTypeException.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Parser/Nodes/ConstantNode.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Parser/Nodes/FunctionNode.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Parser/Nodes/Node.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Parser/Nodes/NodeType.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Parser/Nodes/ProgramNode.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Parser/Nodes/ReturnNode.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Parser/Parser.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$/Compiler.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Compiler.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Lexer.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Lexer/Lexer.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Pattern.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Lexer/Pattern.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Token.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Lexer/Token.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/TokenType.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Lexer/TokenType.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../grammar" beforeDir="false" afterPath="$PROJECT_DIR$/../grammar" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -53,12 +49,24 @@
<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://$PROJECT_DIR$/Lexer/TokenType.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" />
<setting file="file://$PROJECT_DIR$/Parser/Nodes/ConstantNode.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Nodes/FunctionNode.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Nodes/Node.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Nodes/NodeType.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Exceptions/InvalidIdentifierException.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Exceptions/UnexpectedTokenException.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Exceptions/MissingSemicolonException.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Lexer/Lexer.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Compiler.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Exceptions/WrongTypeException.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Lexer/Pattern.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Nodes/ProgramNode.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/EEDBFCB1-C56E-4F7A-8B60-B8DD27E0DC7D/86/71102cca/Enumerable.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Lexer/Token.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Nodes/ReturnNode.cs" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Parser/Parser.cs" root0="FORCE_HIGHLIGHTING" />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
@ -68,9 +76,33 @@
<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" />
<option value="$PROJECT_DIR$/Parser.cs" />
<option value="$PROJECT_DIR$/Lexer/Pattern.cs" />
<option value="$PROJECT_DIR$/Parser/LeafType.cs" />
<option value="$PROJECT_DIR$/Parser/NodeType.cs" />
<option value="$PROJECT_DIR$/Parser/Node.cs" />
<option value="$PROJECT_DIR$/Parser/FunctionNode.cs" />
<option value="$PROJECT_DIR$/Parser/ConstantNode.cs" />
<option value="$PROJECT_DIR$/Parser/ReturnNode.cs" />
<option value="$PROJECT_DIR$/Parser/ProgramNode.cs" />
<option value="$PROJECT_DIR$/Parser/Nodes/ProgramNode.cs" />
<option value="$PROJECT_DIR$/Parser/Exceptions.cs" />
<option value="$PROJECT_DIR$/Parser/Nodes/FunctionNode.cs" />
<option value="$PROJECT_DIR$/Parser/Exceptions/InvalidIdentifierException.cs" />
<option value="$PROJECT_DIR$/Parser/Exceptions/UnexpectedTokenException.cs" />
<option value="$PROJECT_DIR$/Lexer/TokenType.cs" />
<option value="$PROJECT_DIR$/Parser/Exceptions/MissingSemicolonException.cs" />
<option value="$PROJECT_DIR$/Parser/Nodes/ConstantNode.cs" />
<option value="$PROJECT_DIR$/Parser/Exceptions/WronghTypeException.cs" />
<option value="$PROJECT_DIR$/Parser/Exceptions/WrongTypeException.cs" />
<option value="$PROJECT_DIR$/Lexer/Token.cs" />
<option value="$PROJECT_DIR$/Compiler.cs" />
<option value="$PROJECT_DIR$/Parser/Parser.cs" />
<option value="$PROJECT_DIR$/Lexer/Lexer.cs" />
<option value="$PROJECT_DIR$/Parser/Nodes/Node.cs" />
<option value="$PROJECT_DIR$/Parser/Nodes/ReturnNode.cs" />
</list>
</option>
</component>
@ -126,7 +158,8 @@
<option name="presentableId" value="Default" />
<updated>1597507445860</updated>
<workItem from="1597507449039" duration="231000" />
<workItem from="1597508003307" duration="9083000" />
<workItem from="1597508003307" duration="9480000" />
<workItem from="1597604480117" duration="13146000" />
</task>
<servers />
</component>
@ -160,98 +193,102 @@
<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">
<state width="1868" height="383" key="GridCell.Tab.0.bottom" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.0.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.0.center" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.0.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.0.left" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.0.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.0.right" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.0.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.1.bottom" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.1.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.1.center" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.1.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.1.left" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.1.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.1.right" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.1.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.2.bottom" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.2.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.2.center" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.2.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.2.left" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.2.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.2.right" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.2.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.3.bottom" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.3.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.3.center" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.3.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.3.left" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.3.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.3.right" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.3.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.4.bottom" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.4.bottom/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.4.center" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.4.center/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.4.left" timestamp="1597619266025">
<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">
<state width="1868" height="383" key="GridCell.Tab.4.left/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<state width="1868" height="383" key="GridCell.Tab.4.right" timestamp="1597619266025">
<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 width="1868" height="383" key="GridCell.Tab.4.right/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619266025" />
<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">
<state x="2345" y="104" width="709" height="485" key="RiderGenerateDialog" timestamp="1597619025064">
<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="2345" y="104" width="709" height="485" key="RiderGenerateDialog/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597619025064" />
<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 x="2560" y="0" width="640" height="1080" key="VCS.FileHistoryDialog" timestamp="1597605666894">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
<state x="2560" y="0" width="640" height="1080" key="VCS.FileHistoryDialog/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597605666894" />
<state width="480" height="540" key="XDebugger.FullValuePopup" timestamp="1597525606220">
<screen x="1920" y="0" width="1920" height="1080" />
</state>
@ -260,5 +297,13 @@
<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" />
<state x="960" y="557" width="960" height="523" key="dock-window-1" timestamp="1597606916274">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state x="960" y="557" width="960" height="523" key="dock-window-1/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597606916274" />
<state x="960" y="34" width="960" height="523" key="dock-window-2" timestamp="1597606916274">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state x="960" y="34" width="960" height="523" key="dock-window-2/1920.0.1920.1080/0.0.1920.1080@1920.0.1920.1080" timestamp="1597606916274" />
</component>
</project>

View file

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using Compiler.Lexer;
using Compiler.Parser;
namespace Compiler
{
@ -8,14 +10,18 @@ namespace Compiler
{
public static void Main()
{
TestCompiler("../../../../tests/week_1/valid");
TestCompiler("../../../../tests/week_1/invalid");
List<List<Token>> tokensValid = TestLexer("../../../../tests/week_1/valid");
List<List<Token>> tokensInvalid = TestLexer("../../../../tests/week_1/invalid");
TestParser(tokensValid[0]);
TestParser(tokensInvalid[0]);
}
static void TestCompiler(String path)
static List<List<Token>> TestLexer(String path)
{
List<List<Token>> tokenLists = new List<List<Token>>();
String[] files = Directory.GetFiles(path);
Lexer lexer = new Lexer();
Lexer.Lexer lexer = new Lexer.Lexer();
foreach (String filename in files)
{
@ -23,14 +29,25 @@ namespace Compiler
String contents = file.ReadToEnd();
List<Token> tokens = lexer.Lex(contents);
tokenLists.Add(tokens);
Console.WriteLine("-----------" + filename + "-----------");
foreach (Token token in tokens)
{
Console.WriteLine(token.ToString());
}
Console.WriteLine("--------------------------------------");
}
return tokenLists;
}
static void TestParser(List<Token> tokenList)
{
Parser.Parser p = new Parser.Parser();
p.Parse(ref tokenList, NodeType.ProgramNode);
}
}
}

View file

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Compiler
namespace Compiler.Lexer
{
class Lexer
{
@ -70,7 +70,8 @@ namespace Compiler
switch (pattern.GetTokenType())
{
case TokenType.IntegerLiteralToken:
t.Value = m.Value;
//TODO: Error handling is missing here
t.Value = Int32.Parse(m.Value);
break;
case TokenType.IdentifierToken:
t.Value = m.Value;
@ -91,6 +92,7 @@ namespace Compiler
return t;
}
}
//return the next token
return new Token(TokenType.InvalidToken);
}

View file

@ -1,7 +1,7 @@
using System;
using System.Text.RegularExpressions;
namespace Compiler
namespace Compiler.Lexer
{
public class Pattern
{

View file

@ -1,11 +1,12 @@
using System;
using System.Linq.Expressions;
namespace Compiler
namespace Compiler.Lexer
{
public class Token
{
public TokenType TokenType { get; set; }
public String Value { get; set; }
public Object Value { get; set; }
public int Length { get; set; }
public Token(TokenType pTokenType)

View file

@ -1,4 +1,4 @@
namespace Compiler
namespace Compiler.Lexer
{
public enum TokenType
{

View file

@ -0,0 +1,17 @@
using System;
namespace Compiler.Parser.Exceptions
{
public class InvalidIdentifierException : Exception
{
private string value { get; set; }
public override string Message { get; }
public InvalidIdentifierException(string value)
{
this.value = value;
this.Message = "Unexpected Identifier " + this.value + ".";
}
}
}

View file

@ -0,0 +1,9 @@
using System;
namespace Compiler.Parser.Exceptions
{
public class MissingSemicolonException : Exception
{
public override string Message { get; }
}
}

View file

@ -0,0 +1,20 @@
using System;
using Compiler.Lexer;
namespace Compiler.Parser.Exceptions
{
public class UnexpectedTokenException : Exception
{
public TokenType expected { get; set; }
public TokenType got { get; set; }
public override string Message { get; }
public UnexpectedTokenException(TokenType expected, TokenType got)
{
this.expected = expected;
this.got = got;
this.Message = "Unexpected Token " + got + ". Expected: " + expected;
}
}
}

View file

@ -0,0 +1,21 @@
using System;
namespace Compiler.Parser.Exceptions
{
public class WrongTypeException : Exception
{
private Type expected;
private Type got;
public override string Message { get; }
public WrongTypeException(Type expected, Type got)
{
this.expected = expected;
this.got = got;
this.Message = "Expected type " + expected +
"but got type " + got + ".";
}
}
}

View file

@ -0,0 +1,14 @@
namespace Compiler.Parser.Nodes
{
public sealed class ConstantNode : Node
{
public override NodeType NodeType { get; set; }
public int value { get; set; }
public ConstantNode(int value)
{
this.NodeType = NodeType.ExpressionNode;
this.value = value;
}
}
}

View file

@ -0,0 +1,15 @@
using System;
namespace Compiler.Parser.Nodes
{
public sealed class FunctionNode : Node
{
public string Name { get; set; }
public override NodeType NodeType { get; set; }
public FunctionNode()
{
this.NodeType = NodeType.FunctionNode;
}
}
}

View file

@ -0,0 +1,15 @@
using System.Collections.Generic;
namespace Compiler.Parser.Nodes
{
public abstract class Node
{
public abstract NodeType NodeType { get; set; }
public List<Node> Children { get; set; }
protected Node()
{
Children = new List<Node>();
}
}
}

View file

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

View file

@ -0,0 +1,12 @@
namespace Compiler.Parser.Nodes
{
public sealed class ProgramNode : Node
{
public override NodeType NodeType { get; set; }
public ProgramNode()
{
this.NodeType = NodeType.ProgramNode;
}
}
}

View file

@ -0,0 +1,12 @@
namespace Compiler.Parser.Nodes
{
public sealed class ReturnNode : Node
{
public override NodeType NodeType { get; set; }
public ReturnNode()
{
this.NodeType = NodeType.StatementNode;
}
}
}

130
Compiler/Parser/Parser.cs Normal file
View file

@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Compiler.Lexer;
using Compiler.Parser.Exceptions;
using Compiler.Parser.Nodes;
namespace Compiler.Parser
{
public class Parser
{
public Node Parse(ref List<Token> tokenList, NodeType nodeType)
{
var t = tokenList.FirstOrDefault();
if (t == null) throw new Exception("Empty Token List.");
Node n;
switch (nodeType)
{
case NodeType.ProgramNode:
n = new ProgramNode();
//if this node is a program node, the next one must be a function node
Node childNode = Parse(ref tokenList, NodeType.FunctionNode);
n.Children.Add(childNode);
break;
case NodeType.FunctionNode:
n = new FunctionNode();
//retrieve signature and remove it
List<Token> signature = tokenList.GetRange(0, 5);
tokenList.RemoveRange(0, 5);
//check each element of the signature and raise corresponding errors
if (signature[0].TokenType != TokenType.IntToken)
{
throw new UnexpectedTokenException(TokenType.IntToken, signature[1].TokenType);
}
if (signature[1].Value != null)
{
if (signature[1].TokenType == TokenType.IdentifierToken)
{
((FunctionNode) n).Name = signature[1].Value.ToString();
}
else
{
throw new UnexpectedTokenException(TokenType.IdentifierToken, signature[1].TokenType);
}
}
else
{
throw new InvalidIdentifierException(null);
}
if (signature[2].TokenType != TokenType.OpenParenthesisToken)
{
throw new UnexpectedTokenException(TokenType.OpenParenthesisToken, signature[1].TokenType);
}
if (signature[3].TokenType != TokenType.CloseParenthesisToken)
{
throw new UnexpectedTokenException(TokenType.CloseParenthesisToken, signature[1].TokenType);
}
if (signature[4].TokenType != TokenType.OpenBraceToken)
{
throw new UnexpectedTokenException(TokenType.OpenBraceToken, signature[1].TokenType);
}
//add returned child node to AST
n.Children.Add(Parse(ref tokenList, NodeType.StatementNode));
//remove trailing }
tokenList.RemoveAt(0);
break;
case NodeType.StatementNode:
//TODO: This Type of return/statement node will probably need fixing later
n = new ReturnNode();
//get return token and remove it
List<Token> returnStatement = tokenList.GetRange(0, 1);
tokenList.RemoveAt(0);
if (returnStatement[0].TokenType != TokenType.ReturnToken)
{
throw new UnexpectedTokenException(TokenType.ReturnToken, returnStatement[0].TokenType);
}
else
{
//add returned child node to AST
n.Children.Add(Parse(ref tokenList, NodeType.ExpressionNode));
//remove trailing ;
tokenList.RemoveAt(0);
}
break;
case NodeType.ExpressionNode:
Token constantToken = tokenList[0];
//check if TokenType is right
if (constantToken.TokenType != TokenType.IntegerLiteralToken)
{
throw new UnexpectedTokenException(TokenType.IntToken, constantToken.TokenType);
}
else
{
//check if value Type is right
if (constantToken.Value.GetType() != typeof(int))
{
throw new WrongTypeException(typeof(int), constantToken.Value.GetType());
}
//return final constant node to end recursion
n = new ConstantNode((int) constantToken.Value);
}
break;
default:
throw new Exception("Unknown Node Type " + nodeType);
}
return n;
}
}
}

View file

@ -0,0 +1,4 @@
<program> ::= <function>
<function> ::= "int" <id> "(" ")" "{" <statement> "}"
<statement> ::= "return" <exp> ";"
<exp> ::= <int>