From 25372388cb0788a73ebdf92357ee2fd834ba7038 Mon Sep 17 00:00:00 2001 From: Yuuki Wesp Date: Sun, 1 Sep 2024 04:56:02 +0300 Subject: [PATCH] fix --- LLVMSharp.sln | 14 - samples/Directory.Build.props | 13 - samples/Directory.Build.targets | 13 - .../Chapter3/Kaleidoscope.sln | 37 -- .../Kaleidoscope/AST/BinaryExprAST.cs | 42 -- .../Chapter3/Kaleidoscope/AST/CallExprAST.cs | 25 - .../Chapter3/Kaleidoscope/AST/ExprAST.cs | 17 - .../Chapter3/Kaleidoscope/AST/ExprType.cs | 15 - .../Chapter3/Kaleidoscope/AST/ExprVisitor.cs | 65 --- .../Chapter3/Kaleidoscope/AST/FunctionAST.cs | 23 - .../Kaleidoscope/AST/NumberExprAST.cs | 20 - .../Chapter3/Kaleidoscope/AST/PrototypeAST.cs | 25 - .../Kaleidoscope/AST/VariableExprAST.cs | 20 - .../Kaleidoscope/BaseParserListener.cs | 63 --- .../Chapter3/Kaleidoscope/ILexer.cs | 15 - .../Chapter3/Kaleidoscope/IParser.cs | 11 - .../Chapter3/Kaleidoscope/IParserListener.cs | 19 - .../Chapter3/Kaleidoscope/Kaleidoscope.csproj | 6 - .../Chapter3/Kaleidoscope/Lexer.cs | 134 ------ .../Chapter3/Kaleidoscope/Parser.cs | 313 ------------- .../Chapter3/Kaleidoscope/Token.cs | 11 - .../KaleidoscopeLLVM/CodeGenParserListener.cs | 47 -- .../KaleidoscopeLLVM/CodeGenVisitor.cs | 198 -------- .../KaleidoscopeLLVM/KaleidoscopeLLVM.csproj | 14 - .../Chapter3/KaleidoscopeLLVM/Program.cs | 68 --- .../Chapter4/Kaleidoscope.sln | 31 -- .../Kaleidoscope/AST/BinaryExprAST.cs | 42 -- .../Chapter4/Kaleidoscope/AST/CallExprAST.cs | 25 - .../Chapter4/Kaleidoscope/AST/ExprAST.cs | 17 - .../Chapter4/Kaleidoscope/AST/ExprType.cs | 15 - .../Chapter4/Kaleidoscope/AST/ExprVisitor.cs | 60 --- .../Chapter4/Kaleidoscope/AST/FunctionAST.cs | 23 - .../Kaleidoscope/AST/NumberExprAST.cs | 20 - .../Chapter4/Kaleidoscope/AST/PrototypeAST.cs | 25 - .../Kaleidoscope/AST/VariableExprAST.cs | 20 - .../Kaleidoscope/BaseParserListener.cs | 65 --- .../Chapter4/Kaleidoscope/ILexer.cs | 15 - .../Chapter4/Kaleidoscope/IParser.cs | 11 - .../Chapter4/Kaleidoscope/IParserListener.cs | 19 - .../Chapter4/Kaleidoscope/Kaleidoscope.csproj | 6 - .../Chapter4/Kaleidoscope/Lexer.cs | 133 ------ .../Chapter4/Kaleidoscope/Parser.cs | 313 ------------- .../Chapter4/Kaleidoscope/Token.cs | 11 - .../KaleidoscopeLLVM/CodeGenParserListener.cs | 66 --- .../KaleidoscopeLLVM/CodeGenVisitor.cs | 199 -------- .../KaleidoscopeLLVM/KaleidoscopeLLVM.csproj | 14 - .../Chapter4/KaleidoscopeLLVM/Program.cs | 111 ----- .../Chapter5/Kaleidoscope.sln | 37 -- .../Kaleidoscope/AST/BinaryExprAST.cs | 42 -- .../Chapter5/Kaleidoscope/AST/CallExprAST.cs | 25 - .../Chapter5/Kaleidoscope/AST/ExprAST.cs | 17 - .../Chapter5/Kaleidoscope/AST/ExprType.cs | 17 - .../Chapter5/Kaleidoscope/AST/ExprVisitor.cs | 84 ---- .../Chapter5/Kaleidoscope/AST/ForExprAST.cs | 32 -- .../Chapter5/Kaleidoscope/AST/FunctionAST.cs | 23 - .../Chapter5/Kaleidoscope/AST/IfExpAST.cs | 26 -- .../Kaleidoscope/AST/NumberExprAST.cs | 20 - .../Chapter5/Kaleidoscope/AST/PrototypeAST.cs | 25 - .../Kaleidoscope/AST/VariableExprAST.cs | 20 - .../Kaleidoscope/BaseParserListener.cs | 63 --- .../Chapter5/Kaleidoscope/ILexer.cs | 15 - .../Chapter5/Kaleidoscope/IParser.cs | 11 - .../Chapter5/Kaleidoscope/IParserListener.cs | 19 - .../Chapter5/Kaleidoscope/Kaleidoscope.csproj | 6 - .../Chapter5/Kaleidoscope/Lexer.cs | 154 ------- .../Chapter5/Kaleidoscope/Parser.cs | 431 ------------------ .../Chapter5/Kaleidoscope/Token.cs | 16 - .../KaleidoscopeLLVM/CodeGenParserListener.cs | 65 --- .../KaleidoscopeLLVM/CodeGenVisitor.cs | 350 -------------- .../KaleidoscopeLLVM/KaleidoscopeLLVM.csproj | 14 - .../Chapter5/KaleidoscopeLLVM/Program.cs | 114 ----- 71 files changed, 4095 deletions(-) delete mode 100644 samples/Directory.Build.props delete mode 100644 samples/Directory.Build.targets delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope.sln delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/BinaryExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/CallExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/ExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/ExprType.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/ExprVisitor.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/FunctionAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/NumberExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/PrototypeAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/VariableExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/BaseParserListener.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/ILexer.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/IParser.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/IParserListener.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Kaleidoscope.csproj delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Lexer.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Parser.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Token.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/CodeGenParserListener.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/CodeGenVisitor.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/KaleidoscopeLLVM.csproj delete mode 100644 samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/Program.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope.sln delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/BinaryExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/CallExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/ExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/ExprType.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/ExprVisitor.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/FunctionAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/NumberExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/PrototypeAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/VariableExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/BaseParserListener.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/ILexer.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/IParser.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/IParserListener.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Kaleidoscope.csproj delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Lexer.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Parser.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Token.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/CodeGenParserListener.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/CodeGenVisitor.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/KaleidoscopeLLVM.csproj delete mode 100644 samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/Program.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope.sln delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/BinaryExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/CallExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ExprType.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ExprVisitor.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ForExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/FunctionAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/IfExpAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/NumberExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/PrototypeAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/VariableExprAST.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/BaseParserListener.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/ILexer.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/IParser.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/IParserListener.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Kaleidoscope.csproj delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Lexer.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Parser.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Token.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/CodeGenParserListener.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/CodeGenVisitor.cs delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/KaleidoscopeLLVM.csproj delete mode 100644 samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/Program.cs diff --git a/LLVMSharp.sln b/LLVMSharp.sln index 3e3f3d1b..b3e28ae2 100644 --- a/LLVMSharp.sln +++ b/LLVMSharp.sln @@ -63,10 +63,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{ .github\workflows\ci.yml = .github\workflows\ci.yml EndProjectSection EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kaleidoscope", "samples\KaleidoscopeTutorial\Chapter3\Kaleidoscope\Kaleidoscope.csproj", "{D27EBCA1-AA49-46BD-B9C3-FDAC5858EAD9}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KaleidoscopeLLVM", "samples\KaleidoscopeTutorial\Chapter3\KaleidoscopeLLVM\KaleidoscopeLLVM.csproj", "{A6F6EE4E-B036-4335-AEA7-8BBA9E339116}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -85,14 +81,6 @@ Global {CEA0F0C9-DF25-446B-8F55-FB629BD1D3E6}.Debug|Any CPU.Build.0 = Debug|Any CPU {CEA0F0C9-DF25-446B-8F55-FB629BD1D3E6}.Release|Any CPU.ActiveCfg = Release|Any CPU {CEA0F0C9-DF25-446B-8F55-FB629BD1D3E6}.Release|Any CPU.Build.0 = Release|Any CPU - {D27EBCA1-AA49-46BD-B9C3-FDAC5858EAD9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D27EBCA1-AA49-46BD-B9C3-FDAC5858EAD9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D27EBCA1-AA49-46BD-B9C3-FDAC5858EAD9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D27EBCA1-AA49-46BD-B9C3-FDAC5858EAD9}.Release|Any CPU.Build.0 = Release|Any CPU - {A6F6EE4E-B036-4335-AEA7-8BBA9E339116}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A6F6EE4E-B036-4335-AEA7-8BBA9E339116}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A6F6EE4E-B036-4335-AEA7-8BBA9E339116}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A6F6EE4E-B036-4335-AEA7-8BBA9E339116}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -102,8 +90,6 @@ Global {293C5F55-2CB1-4398-946B-8AD8E37D7FEE} = {5E2DA015-A829-4304-9B2F-B868ECB77A95} {CEA0F0C9-DF25-446B-8F55-FB629BD1D3E6} = {62B5C536-C224-4EE1-972F-05F732B5980B} {706334B8-BB91-49A4-BC9C-DFFDF3DCD770} = {E8ADE5A7-4363-43B7-B104-6B0E5510CAF0} - {D27EBCA1-AA49-46BD-B9C3-FDAC5858EAD9} = {62B5C536-C224-4EE1-972F-05F732B5980B} - {A6F6EE4E-B036-4335-AEA7-8BBA9E339116} = {62B5C536-C224-4EE1-972F-05F732B5980B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {75550D8C-A492-4ED3-8387-206830F8B21E} diff --git a/samples/Directory.Build.props b/samples/Directory.Build.props deleted file mode 100644 index 3ab62476..00000000 --- a/samples/Directory.Build.props +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - diff --git a/samples/Directory.Build.targets b/samples/Directory.Build.targets deleted file mode 100644 index 2cfa2bcb..00000000 --- a/samples/Directory.Build.targets +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope.sln b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope.sln deleted file mode 100644 index eae9808a..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope.sln +++ /dev/null @@ -1,37 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.31101.0 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kaleidoscope", "Kaleidoscope\Kaleidoscope.csproj", "{CD23CB2E-951B-4FAD-A4CD-4048731DBA31}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KaleidoscopeLLVM", "KaleidoscopeLLVM\KaleidoscopeLLVM.csproj", "{C1147A13-4174-410A-A891-335D858703B7}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Debug|x64 = Debug|x64 - Release|Any CPU = Release|Any CPU - Release|x64 = Release|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Debug|x64.ActiveCfg = Debug|x64 - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Debug|x64.Build.0 = Debug|x64 - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Release|Any CPU.Build.0 = Release|Any CPU - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Release|x64.ActiveCfg = Release|x64 - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Release|x64.Build.0 = Release|x64 - {C1147A13-4174-410A-A891-335D858703B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C1147A13-4174-410A-A891-335D858703B7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C1147A13-4174-410A-A891-335D858703B7}.Debug|x64.ActiveCfg = Debug|x64 - {C1147A13-4174-410A-A891-335D858703B7}.Debug|x64.Build.0 = Debug|x64 - {C1147A13-4174-410A-A891-335D858703B7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C1147A13-4174-410A-A891-335D858703B7}.Release|Any CPU.Build.0 = Release|Any CPU - {C1147A13-4174-410A-A891-335D858703B7}.Release|x64.ActiveCfg = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/BinaryExprAST.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/BinaryExprAST.cs deleted file mode 100644 index 1143f724..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/BinaryExprAST.cs +++ /dev/null @@ -1,42 +0,0 @@ -namespace Kaleidoscope.AST -{ - using System; - - public sealed class BinaryExprAST : ExprAST - { - public BinaryExprAST(char op, ExprAST lhs, ExprAST rhs) - { - switch (op) - { - case '+': - this.NodeType = ExprType.AddExpr; - break; - case '-': - this.NodeType = ExprType.SubtractExpr; - break; - case '*': - this.NodeType = ExprType.MultiplyExpr; - break; - case '<': - this.NodeType = ExprType.LessThanExpr; - break; - default: - throw new ArgumentException("op " + op + " is not a valid operator"); - } - - this.Lhs = lhs; - this.Rhs = rhs; - } - - public ExprAST Lhs { get; private set; } - - public ExprAST Rhs { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitBinaryExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/CallExprAST.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/CallExprAST.cs deleted file mode 100644 index 1a722b27..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/CallExprAST.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace Kaleidoscope.AST -{ - using System.Collections.Generic; - - public sealed class CallExprAST : ExprAST - { - public CallExprAST(string callee, List args) - { - this.Callee = callee; - this.Arguments = args; - this.NodeType = ExprType.CallExpr; - } - - public string Callee { get; private set; } - - public List Arguments { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitCallExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/ExprAST.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/ExprAST.cs deleted file mode 100644 index 6841cbec..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/ExprAST.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Kaleidoscope.AST -{ - public abstract class ExprAST - { - public abstract ExprType NodeType { get; protected set; } - - protected internal virtual ExprAST VisitChildren(ExprVisitor visitor) - { - return visitor.Visit(this); - } - - protected internal virtual ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitExtension(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/ExprType.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/ExprType.cs deleted file mode 100644 index e890f346..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/ExprType.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Kaleidoscope.AST -{ - public enum ExprType - { - AddExpr, - SubtractExpr, - MultiplyExpr, - LessThanExpr, - CallExpr, - VariableExpr, - PrototypeExpr, - FunctionExpr, - NumberExpr - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/ExprVisitor.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/ExprVisitor.cs deleted file mode 100644 index a51f80cc..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/ExprVisitor.cs +++ /dev/null @@ -1,65 +0,0 @@ -namespace Kaleidoscope.AST -{ - public abstract class ExprVisitor - { - protected ExprVisitor() - { - } - - public virtual ExprAST Visit(ExprAST node) - { - if (node != null) - { - return node.Accept(this); - } - - return null; - } - - protected internal virtual ExprAST VisitExtension(ExprAST node) - { - return node.VisitChildren(this); - } - - protected internal virtual ExprAST VisitBinaryExprAST(BinaryExprAST node) - { - this.Visit(node.Lhs); - this.Visit(node.Rhs); - - return node; - } - - protected internal virtual ExprAST VisitCallExprAST(CallExprAST node) - { - foreach (var argument in node.Arguments) - { - this.Visit(argument); - } - - return node; - } - - protected internal virtual ExprAST VisitFunctionAST(FunctionAST node) - { - this.Visit(node.Proto); - this.Visit(node.Body); - - return node; - } - - protected internal virtual ExprAST VisitVariableExprAST(VariableExprAST node) - { - return node; - } - - protected internal virtual ExprAST VisitPrototypeAST(PrototypeAST node) - { - return node; - } - - protected internal virtual ExprAST VisitNumberExprAST(NumberExprAST node) - { - return node; - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/FunctionAST.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/FunctionAST.cs deleted file mode 100644 index a5e90405..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/FunctionAST.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace Kaleidoscope.AST -{ - public sealed class FunctionAST : ExprAST - { - public FunctionAST(PrototypeAST proto, ExprAST body) - { - this.Proto = proto; - this.Body = body; - this.NodeType = ExprType.FunctionExpr; - } - - public PrototypeAST Proto { get; private set; } - - public ExprAST Body { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitFunctionAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/NumberExprAST.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/NumberExprAST.cs deleted file mode 100644 index bd823cd3..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/NumberExprAST.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace Kaleidoscope.AST -{ - public sealed class NumberExprAST : ExprAST - { - public NumberExprAST(double value) - { - this.Value = value; - this.NodeType = ExprType.NumberExpr; - } - - public double Value { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitNumberExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/PrototypeAST.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/PrototypeAST.cs deleted file mode 100644 index 3d0f6b4a..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/PrototypeAST.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace Kaleidoscope.AST -{ - using System.Collections.Generic; - - public sealed class PrototypeAST : ExprAST - { - public PrototypeAST(string name, List args) - { - this.Name = name; - this.Arguments = args; - this.NodeType = ExprType.PrototypeExpr; - } - - public string Name { get; private set; } - - public List Arguments { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitPrototypeAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/VariableExprAST.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/VariableExprAST.cs deleted file mode 100644 index 41aeb6c2..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/AST/VariableExprAST.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace Kaleidoscope.AST -{ - public sealed class VariableExprAST : ExprAST - { - public VariableExprAST(string name) - { - this.Name = name; - this.NodeType = ExprType.VariableExpr; - } - - public string Name { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitVariableExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/BaseParserListener.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/BaseParserListener.cs deleted file mode 100644 index 9eb96731..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/BaseParserListener.cs +++ /dev/null @@ -1,63 +0,0 @@ -namespace Kaleidoscope -{ - using System; - using System.Collections.Generic; - using System.Reflection; - using AST; - - internal sealed class BaseParserListener - { - private static readonly Type IParserListenerType = typeof(IParserListener); - - private readonly Stack descentStack = new Stack(); - - private readonly Stack ascentStack = new Stack(); - - private readonly IParserListener listener; - - public BaseParserListener(IParserListener listener) - { - this.listener = listener; - } - - public void EnterRule(string ruleName) - { - this.descentStack.Push(ruleName); - } - - public void ExitRule(ExprAST argument) - { - string ruleName = this.descentStack.Pop(); - this.ascentStack.Push(new ASTContext(IParserListenerType.GetMethod("Exit" + ruleName), this.listener, argument)); - this.ascentStack.Push(new ASTContext(IParserListenerType.GetMethod("Enter" + ruleName), this.listener, argument)); - } - - public void Listen() - { - if (this.listener != null) - { - while (this.ascentStack.Count != 0) - { - var context = this.ascentStack.Pop(); - context.MethodInfo.Invoke(context.Instance, new object[] { context.Argument }); - } - } - } - - private sealed class ASTContext - { - public ASTContext(MethodInfo methodInfo, object instance, ExprAST argument) - { - this.MethodInfo = methodInfo; - this.Instance = instance; - this.Argument = argument; - } - - public MethodInfo MethodInfo { get; private set; } - - public ExprAST Argument { get; set; } - - public object Instance { get; private set; } - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/ILexer.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/ILexer.cs deleted file mode 100644 index aca5d18f..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/ILexer.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Kaleidoscope -{ - public interface ILexer - { - int CurrentToken { get; } - - string GetLastIdentifier(); - - double GetLastNumber(); - - int GetTokPrecedence(); - - int GetNextToken(); - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/IParser.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/IParser.cs deleted file mode 100644 index adf84705..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/IParser.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Kaleidoscope -{ - public interface IParser - { - void HandleDefinition(); - - void HandleExtern(); - - void HandleTopLevelExpression(); - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/IParserListener.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/IParserListener.cs deleted file mode 100644 index fbc91f82..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/IParserListener.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace Kaleidoscope -{ - using AST; - - public interface IParserListener - { - void EnterHandleDefinition(FunctionAST data); - - void ExitHandleDefinition(FunctionAST data); - - void EnterHandleExtern(PrototypeAST data); - - void ExitHandleExtern(PrototypeAST data); - - void EnterHandleTopLevelExpression(FunctionAST data); - - void ExitHandleTopLevelExpression(FunctionAST data); - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Kaleidoscope.csproj b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Kaleidoscope.csproj deleted file mode 100644 index 0c3ad700..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Kaleidoscope.csproj +++ /dev/null @@ -1,6 +0,0 @@ - - - - net8.0 - - diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Lexer.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Lexer.cs deleted file mode 100644 index fc321d68..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Lexer.cs +++ /dev/null @@ -1,134 +0,0 @@ -namespace Kaleidoscope -{ - using System; - using System.Collections.Generic; - using System.Globalization; - using System.IO; - using System.Text; - - public sealed class Lexer : ILexer - { - private const int EOF = -1; - - private readonly TextReader reader; - - private readonly StringBuilder identifierBuilder = new StringBuilder(); - - private readonly StringBuilder numberBuilder = new StringBuilder(); - - private readonly Dictionary binopPrecedence; - - private int c = ' '; - - private string identifier; - - private double numVal; - - public Lexer(TextReader reader, Dictionary binOpPrecedence) - { - this.reader = reader; - this.binopPrecedence = binOpPrecedence; - } - - public int CurrentToken { get; private set; } - - public string GetLastIdentifier() - { - return this.identifier; - } - - public double GetLastNumber() - { - return this.numVal; - } - - public int GetTokPrecedence() - { - // Make sure it's a declared binop. - int tokPrec; - if (this.binopPrecedence.TryGetValue((char)this.CurrentToken, out tokPrec)) - { - return tokPrec; - } - - return -1; - } - - public int GetNextToken() - { - // Skip any whitespace. - while (char.IsWhiteSpace((char)c)) - { - c = this.reader.Read(); - } - - if (char.IsLetter((char)c)) // identifier: [a-zA-Z][a-zA-Z0-9]* - { - this.identifierBuilder.Append((char)c); - while (char.IsLetterOrDigit((char)(c = this.reader.Read()))) - { - this.identifierBuilder.Append((char)c); - } - - this.identifier = this.identifierBuilder.ToString(); - this.identifierBuilder.Clear(); - - if (string.Equals(identifier, "def", StringComparison.Ordinal)) - { - this.CurrentToken = (int)Token.DEF; - } - else if (string.Equals(identifier, "extern", StringComparison.Ordinal)) - { - this.CurrentToken = (int)Token.EXTERN; - } - else - { - this.CurrentToken = (int)Token.IDENTIFIER; - } - - return this.CurrentToken; - } - - // Number: [0-9.]+ - if (char.IsDigit((char)c) || c == '.') - { - do - { - this.numberBuilder.Append((char)c); - c = this.reader.Read(); - } while (char.IsDigit((char)c) || c == '.'); - - this.numVal = double.Parse(this.numberBuilder.ToString()); - this.numberBuilder.Clear(); - this.CurrentToken = (int)Token.NUMBER; - - return this.CurrentToken; - } - - if (c == '#') - { - // Comment until end of line. - do - { - c = this.reader.Read(); - } while (c != EOF && c != '\n' && c != '\r'); - - if (c != EOF) - { - return this.GetNextToken(); - } - } - - // Check for end of file. Don't eat the EOF. - if (c == EOF) - { - this.CurrentToken = c; - return (int)Token.EOF; - } - - this.CurrentToken = c; - c = this.reader.Read(); - return this.c; - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Parser.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Parser.cs deleted file mode 100644 index 15954149..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Parser.cs +++ /dev/null @@ -1,313 +0,0 @@ -namespace Kaleidoscope -{ - using System; - using System.Collections.Generic; - using AST; - - public sealed class Parser : IParser - { - private readonly Lexer scanner; - - private readonly BaseParserListener baseListener; - - public Parser(Lexer scanner, IParserListener listener) - { - this.scanner = scanner; - this.baseListener = new BaseParserListener(listener); - } - - public void HandleDefinition() - { - this.baseListener.EnterRule("HandleDefinition"); - - var functionAST = this.ParseDefinition(); - - baseListener.ExitRule(functionAST); - - if (functionAST != null) - { - this.baseListener.Listen(); - } - else - { - // Skip token for error recovery. - this.scanner.GetNextToken(); - } - } - - public void HandleExtern() - { - this.baseListener.EnterRule("HandleExtern"); - - var prototypeAST = this.ParseExtern(); - - this.baseListener.ExitRule(prototypeAST); - - if (prototypeAST != null) - { - this.baseListener.Listen(); - } - else - { - // Skip token for error recovery. - this.scanner.GetNextToken(); - } - } - - public void HandleTopLevelExpression() - { - // Evaluate a top-level expression into an anonymous function. - this.baseListener.EnterRule("HandleTopLevelExpression"); - - var functionAST = this.ParseTopLevelExpr(); - - this.baseListener.ExitRule(functionAST); - - if (functionAST != null) - { - this.baseListener.Listen(); - } - else - { - // Skip token for error recovery. - this.scanner.GetNextToken(); - } - } - - // identifierexpr - // ::= identifier - // ::= identifier '(' expression* ')' - private ExprAST ParseIdentifierExpr() - { - string idName = this.scanner.GetLastIdentifier(); - - this.scanner.GetNextToken(); // eat identifier. - - if (this.scanner.CurrentToken != '(') // Simple variable ref. - { - return new VariableExprAST(idName); - } - - // Call. - this.scanner.GetNextToken(); // eat ( - List args = new List(); - - if (this.scanner.CurrentToken != ')') - { - while (true) - { - ExprAST arg = this.ParseExpression(); - if (arg == null) - { - return null; - } - - args.Add(arg); - - if (this.scanner.CurrentToken == ')') - { - break; - } - - if (this.scanner.CurrentToken != ',') - { - Console.WriteLine("Expected ')' or ',' in argument list"); - return null; - } - - this.scanner.GetNextToken(); - } - } - - // Eat the ')'. - this.scanner.GetNextToken(); - - return new CallExprAST(idName, args); - } - - // numberexpr ::= number - private ExprAST ParseNumberExpr() - { - ExprAST result = new NumberExprAST(this.scanner.GetLastNumber()); - this.scanner.GetNextToken(); - return result; - } - - // parenexpr ::= '(' expression ')' - private ExprAST ParseParenExpr() - { - this.scanner.GetNextToken(); // eat (. - ExprAST v = this.ParseExpression(); - if (v == null) - { - return null; - } - - if (this.scanner.CurrentToken != ')') - { - Console.WriteLine("expected ')'"); - return null; - } - - this.scanner.GetNextToken(); // eat ). - - return v; - } - - // primary - // ::= identifierexpr - // ::= numberexpr - // ::= parenexpr - private ExprAST ParsePrimary() - { - switch (this.scanner.CurrentToken) - { - case (int)Token.IDENTIFIER: - return this.ParseIdentifierExpr(); - case (int)Token.NUMBER: - return this.ParseNumberExpr(); - case '(': - return this.ParseParenExpr(); - default: - Console.WriteLine("unknown token when expecting an expression"); - return null; - } - } - - // binoprhs - // ::= ('+' primary)* - private ExprAST ParseBinOpRHS(int exprPrec, ExprAST lhs) - { - // If this is a binop, find its precedence. - while (true) - { - int tokPrec = this.scanner.GetTokPrecedence(); - - // If this is a binop that binds at least as tightly as the current binop, - // consume it, otherwise we are done. - if (tokPrec < exprPrec) - { - return lhs; - } - - // Okay, we know this is a binop. - int binOp = this.scanner.CurrentToken; - this.scanner.GetNextToken(); // eat binop - - // Parse the primary expression after the binary operator. - ExprAST rhs = this.ParsePrimary(); - if (rhs == null) - { - return null; - } - - // If BinOp binds less tightly with RHS than the operator after RHS, let - // the pending operator take RHS as its LHS. - int nextPrec = this.scanner.GetTokPrecedence(); - if (tokPrec < nextPrec) - { - rhs = this.ParseBinOpRHS(tokPrec + 1, rhs); - if (rhs == null) - { - return null; - } - } - - // Merge LHS/RHS. - lhs = new BinaryExprAST((char)binOp, lhs, rhs); - } - } - - // expression - // ::= primary binoprhs - // - private ExprAST ParseExpression() - { - ExprAST lhs = this.ParsePrimary(); - if (lhs == null) - { - return null; - } - - return this.ParseBinOpRHS(0, lhs); - } - - // prototype - // ::= id '(' id* ')' - private PrototypeAST ParsePrototype() - { - if (this.scanner.CurrentToken != (int)Token.IDENTIFIER) - { - Console.WriteLine("Expected function name in prototype"); - return null; - } - - string fnName = this.scanner.GetLastIdentifier(); - - this.scanner.GetNextToken(); - - if (this.scanner.CurrentToken != '(') - { - Console.WriteLine("Expected '(' in prototype"); - return null; - } - - List argNames = new List(); - while (this.scanner.GetNextToken() == (int)Token.IDENTIFIER) - { - argNames.Add(this.scanner.GetLastIdentifier()); - } - - if (this.scanner.CurrentToken != ')') - { - Console.WriteLine("Expected ')' in prototype"); - return null; - } - - this.scanner.GetNextToken(); // eat ')'. - - return new PrototypeAST(fnName, argNames); - } - - // definition ::= 'def' prototype expression - private FunctionAST ParseDefinition() - { - this.scanner.GetNextToken(); // eat def. - PrototypeAST proto = this.ParsePrototype(); - - if (proto == null) - { - return null; - } - - ExprAST body = this.ParseExpression(); - if (body == null) - { - return null; - } - - return new FunctionAST(proto, body); - } - - /// toplevelexpr ::= expression - private FunctionAST ParseTopLevelExpr() - { - ExprAST e = this.ParseExpression(); - if (e == null) - { - return null; - } - - // Make an anonymous proto. - PrototypeAST proto = new PrototypeAST(string.Empty, new List()); - return new FunctionAST(proto, e); - } - - /// external ::= 'extern' prototype - private PrototypeAST ParseExtern() - { - this.scanner.GetNextToken(); // eat extern. - return this.ParsePrototype(); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Token.cs b/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Token.cs deleted file mode 100644 index a4f84c23..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/Kaleidoscope/Token.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Kaleidoscope -{ - public enum Token - { - EOF = -1, - DEF = -2, - EXTERN = -3, - IDENTIFIER = -4, - NUMBER = -5, - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/CodeGenParserListener.cs b/samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/CodeGenParserListener.cs deleted file mode 100644 index ed23a774..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/CodeGenParserListener.cs +++ /dev/null @@ -1,47 +0,0 @@ -using LLVMSharp; - -namespace KaleidoscopeLLVM -{ - using Kaleidoscope; - using Kaleidoscope.AST; - - internal sealed class CodeGenParserListener : IParserListener - { - private readonly CodeGenVisitor visitor; - - public CodeGenParserListener(CodeGenVisitor visitor) - { - this.visitor = visitor; - } - - public void EnterHandleDefinition(FunctionAST data) - { - } - - public void ExitHandleDefinition(FunctionAST data) - { - this.visitor.Visit(data); - LLVM.DumpValue(this.visitor.ResultStack.Pop()); - } - - public void EnterHandleExtern(PrototypeAST data) - { - } - - public void ExitHandleExtern(PrototypeAST data) - { - this.visitor.Visit(data); - LLVM.DumpValue(this.visitor.ResultStack.Pop()); - } - - public void EnterHandleTopLevelExpression(FunctionAST data) - { - } - - public void ExitHandleTopLevelExpression(FunctionAST data) - { - this.visitor.Visit(data); - LLVM.DumpValue(this.visitor.ResultStack.Pop()); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/CodeGenVisitor.cs b/samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/CodeGenVisitor.cs deleted file mode 100644 index f5aa974a..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/CodeGenVisitor.cs +++ /dev/null @@ -1,198 +0,0 @@ -using System; -using System.Collections.Generic; -using Kaleidoscope.AST; -using LLVMSharp; - -namespace KaleidoscopeLLVM -{ - internal sealed class CodeGenVisitor : ExprVisitor - { - private static readonly LLVMBool LLVMBoolFalse = new LLVMBool(0); - - private static readonly LLVMValueRef NullValue = new LLVMValueRef(IntPtr.Zero); - - private readonly LLVMModuleRef module; - - private readonly LLVMBuilderRef builder; - - private readonly Dictionary namedValues = new Dictionary(); - - private readonly Stack valueStack = new Stack(); - - public CodeGenVisitor(LLVMModuleRef module, LLVMBuilderRef builder) - { - this.module = module; - this.builder = builder; - } - - public Stack ResultStack { get { return valueStack; } } - - public void ClearResultStack() - { - this.valueStack.Clear(); - } - - protected override ExprAST VisitNumberExprAST(NumberExprAST node) - { - this.valueStack.Push(LLVM.ConstReal(LLVM.DoubleType(), node.Value)); - return node; - } - - protected override ExprAST VisitVariableExprAST(VariableExprAST node) - { - LLVMValueRef value; - - // Look this variable up in the function. - if (this.namedValues.TryGetValue(node.Name, out value)) - { - this.valueStack.Push(value); - } - else - { - throw new Exception("Unknown variable name"); - } - - return node; - } - - protected override ExprAST VisitBinaryExprAST(BinaryExprAST node) - { - this.Visit(node.Lhs); - this.Visit(node.Rhs); - - LLVMValueRef r = this.valueStack.Pop(); - LLVMValueRef l = this.valueStack.Pop(); - - LLVMValueRef n; - - switch (node.NodeType) - { - case ExprType.AddExpr: - n = LLVM.BuildFAdd(this.builder, l, r, "addtmp"); - break; - case ExprType.SubtractExpr: - n = LLVM.BuildFSub(this.builder, l, r, "subtmp"); - break; - case ExprType.MultiplyExpr: - n = LLVM.BuildFMul(this.builder, l, r, "multmp"); - break; - case ExprType.LessThanExpr: - // Convert bool 0/1 to double 0.0 or 1.0 - n = LLVM.BuildUIToFP(this.builder, LLVM.BuildFCmp(this.builder, LLVMRealPredicate.LLVMRealULT, l, r, "cmptmp"), LLVM.DoubleType(), "booltmp"); - break; - default: - throw new Exception("invalid binary operator"); - } - - this.valueStack.Push(n); - return node; - } - - protected override ExprAST VisitCallExprAST(CallExprAST node) - { - var calleeF = LLVM.GetNamedFunction(this.module, node.Callee); - if (calleeF.Pointer == IntPtr.Zero) - { - throw new Exception("Unknown function referenced"); - } - - if (LLVM.CountParams(calleeF) != node.Arguments.Count) - { - throw new Exception("Incorrect # arguments passed"); - } - - var argumentCount = (uint)node.Arguments.Count; - var argsV = new LLVMValueRef[Math.Max(argumentCount, 1)]; - for (int i = 0; i < argumentCount; ++i) - { - this.Visit(node.Arguments[i]); - argsV[i] = this.valueStack.Pop(); - } - - valueStack.Push(LLVM.BuildCall(this.builder, calleeF, argsV, "calltmp")); - - return node; - } - - protected override ExprAST VisitPrototypeAST(PrototypeAST node) - { - // Make the function type: double(double,double) etc. - var argumentCount = (uint)node.Arguments.Count; - var arguments = new LLVMTypeRef[Math.Max(argumentCount, 1)]; - - var function = LLVM.GetNamedFunction(this.module, node.Name); - - // If F conflicted, there was already something named 'Name'. If it has a - // body, don't allow redefinition or reextern. - if (function.Pointer != IntPtr.Zero) - { - // If F already has a body, reject this. - if (LLVM.CountBasicBlocks(function) != 0) - { - throw new Exception("redefinition of function."); - } - - // If F took a different number of args, reject. - if (LLVM.CountParams(function) != argumentCount) - { - throw new Exception("redefinition of function with different # args"); - } - } - else - { - for (int i = 0; i < argumentCount; ++i) - { - arguments[i] = LLVM.DoubleType(); - } - - function = LLVM.AddFunction(this.module, node.Name, LLVM.FunctionType(LLVM.DoubleType(), arguments, LLVMBoolFalse)); - LLVM.SetLinkage(function, LLVMLinkage.LLVMExternalLinkage); - } - - for (int i = 0; i < argumentCount; ++i) - { - string argumentName = node.Arguments[i]; - - LLVMValueRef param = LLVM.GetParam(function, (uint)i); - LLVM.SetValueName(param, argumentName); - - this.namedValues[argumentName] = param; - } - - this.valueStack.Push(function); - return node; - } - - protected override ExprAST VisitFunctionAST(FunctionAST node) - { - this.namedValues.Clear(); - - this.Visit(node.Proto); - - LLVMValueRef function = this.valueStack.Pop(); - - // Create a new basic block to start insertion into. - LLVM.PositionBuilderAtEnd(this.builder, LLVM.AppendBasicBlock(function, "entry")); - - try - { - this.Visit(node.Body); - } - catch (Exception) - { - LLVM.DeleteFunction(function); - throw; - } - - // Finish off the function. - LLVM.BuildRet(this.builder, this.valueStack.Pop()); - - // Validate the generated code, checking for consistency. - LLVM.VerifyFunction(function, LLVMVerifierFailureAction.LLVMPrintMessageAction); - - this.valueStack.Push(function); - - return node; - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/KaleidoscopeLLVM.csproj b/samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/KaleidoscopeLLVM.csproj deleted file mode 100644 index 3bf1f997..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/KaleidoscopeLLVM.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - net8.0 - - - - - - - - - - diff --git a/samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/Program.cs b/samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/Program.cs deleted file mode 100644 index 91acc031..00000000 --- a/samples/KaleidoscopeTutorial/Chapter3/KaleidoscopeLLVM/Program.cs +++ /dev/null @@ -1,68 +0,0 @@ -namespace KaleidoscopeLLVM -{ - using System; - using System.Collections.Generic; - using Kaleidoscope; - using LLVMSharp; - - public sealed class Program - { - private static void Main(string[] args) - { - // Make the module, which holds all the code. - LLVMModuleRef module = LLVM.ModuleCreateWithName("my cool jit"); - LLVMBuilderRef builder = LLVM.CreateBuilder(); - var codeGenlistener = new CodeGenParserListener(new CodeGenVisitor(module, builder)); - - // Install standard binary operators. - // 1 is lowest precedence. - var binopPrecedence = new Dictionary - { - ['<'] = 10, - ['+'] = 20, - ['-'] = 20, - ['*'] = 40 - }; - // highest. - - var scanner = new Lexer(Console.In, binopPrecedence); - var parser = new Parser(scanner, codeGenlistener); - - // Prime the first token. - Console.Write("ready> "); - scanner.GetNextToken(); - - // Run the main "interpreter loop" now. - MainLoop(scanner, parser); - - // Print out all of the generated code. - LLVM.DumpModule(module); - } - - private static void MainLoop(ILexer lexer, IParser parser) - { - // top ::= definition | external | expression | ';' - while (true) - { - Console.Write("ready> "); - switch (lexer.CurrentToken) - { - case (int)Token.EOF: - return; - case ';': - lexer.GetNextToken(); - break; - case (int)Token.DEF: - parser.HandleDefinition(); - break; - case (int)Token.EXTERN: - parser.HandleExtern(); - break; - default: - parser.HandleTopLevelExpression(); - break; - } - } - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope.sln b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope.sln deleted file mode 100644 index 1bb9c05b..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.31101.0 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kaleidoscope", "Kaleidoscope\Kaleidoscope.csproj", "{CD23CB2E-951B-4FAD-A4CD-4048731DBA31}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KaleidoscopeLLVM", "KaleidoscopeLLVM\KaleidoscopeLLVM.csproj", "{C1147A13-4174-410A-A891-335D858703B7}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Debug|x64 = Debug|x64 - Release|Any CPU = Release|Any CPU - Release|x64 = Release|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Release|Any CPU.Build.0 = Release|Any CPU - {C1147A13-4174-410A-A891-335D858703B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C1147A13-4174-410A-A891-335D858703B7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C1147A13-4174-410A-A891-335D858703B7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C1147A13-4174-410A-A891-335D858703B7}.Release|Any CPU.Build.0 = Release|Any CPU - {C1147A13-4174-410A-A891-335D858703B7}.Release|x64.ActiveCfg = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/BinaryExprAST.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/BinaryExprAST.cs deleted file mode 100644 index 1143f724..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/BinaryExprAST.cs +++ /dev/null @@ -1,42 +0,0 @@ -namespace Kaleidoscope.AST -{ - using System; - - public sealed class BinaryExprAST : ExprAST - { - public BinaryExprAST(char op, ExprAST lhs, ExprAST rhs) - { - switch (op) - { - case '+': - this.NodeType = ExprType.AddExpr; - break; - case '-': - this.NodeType = ExprType.SubtractExpr; - break; - case '*': - this.NodeType = ExprType.MultiplyExpr; - break; - case '<': - this.NodeType = ExprType.LessThanExpr; - break; - default: - throw new ArgumentException("op " + op + " is not a valid operator"); - } - - this.Lhs = lhs; - this.Rhs = rhs; - } - - public ExprAST Lhs { get; private set; } - - public ExprAST Rhs { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitBinaryExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/CallExprAST.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/CallExprAST.cs deleted file mode 100644 index 1a722b27..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/CallExprAST.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace Kaleidoscope.AST -{ - using System.Collections.Generic; - - public sealed class CallExprAST : ExprAST - { - public CallExprAST(string callee, List args) - { - this.Callee = callee; - this.Arguments = args; - this.NodeType = ExprType.CallExpr; - } - - public string Callee { get; private set; } - - public List Arguments { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitCallExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/ExprAST.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/ExprAST.cs deleted file mode 100644 index 6841cbec..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/ExprAST.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Kaleidoscope.AST -{ - public abstract class ExprAST - { - public abstract ExprType NodeType { get; protected set; } - - protected internal virtual ExprAST VisitChildren(ExprVisitor visitor) - { - return visitor.Visit(this); - } - - protected internal virtual ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitExtension(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/ExprType.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/ExprType.cs deleted file mode 100644 index e890f346..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/ExprType.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Kaleidoscope.AST -{ - public enum ExprType - { - AddExpr, - SubtractExpr, - MultiplyExpr, - LessThanExpr, - CallExpr, - VariableExpr, - PrototypeExpr, - FunctionExpr, - NumberExpr - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/ExprVisitor.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/ExprVisitor.cs deleted file mode 100644 index a357e1d4..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/ExprVisitor.cs +++ /dev/null @@ -1,60 +0,0 @@ -namespace Kaleidoscope.AST -{ - public abstract class ExprVisitor - { - protected ExprVisitor() - { - } - - public virtual ExprAST Visit(ExprAST node) - { - return node?.Accept(this); - } - - protected internal virtual ExprAST VisitExtension(ExprAST node) - { - return node.VisitChildren(this); - } - - protected internal virtual ExprAST VisitBinaryExprAST(BinaryExprAST node) - { - this.Visit(node.Lhs); - this.Visit(node.Rhs); - - return node; - } - - protected internal virtual ExprAST VisitCallExprAST(CallExprAST node) - { - foreach (var argument in node.Arguments) - { - this.Visit(argument); - } - - return node; - } - - protected internal virtual ExprAST VisitFunctionAST(FunctionAST node) - { - this.Visit(node.Proto); - this.Visit(node.Body); - - return node; - } - - protected internal virtual ExprAST VisitVariableExprAST(VariableExprAST node) - { - return node; - } - - protected internal virtual ExprAST VisitPrototypeAST(PrototypeAST node) - { - return node; - } - - protected internal virtual ExprAST VisitNumberExprAST(NumberExprAST node) - { - return node; - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/FunctionAST.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/FunctionAST.cs deleted file mode 100644 index a5e90405..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/FunctionAST.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace Kaleidoscope.AST -{ - public sealed class FunctionAST : ExprAST - { - public FunctionAST(PrototypeAST proto, ExprAST body) - { - this.Proto = proto; - this.Body = body; - this.NodeType = ExprType.FunctionExpr; - } - - public PrototypeAST Proto { get; private set; } - - public ExprAST Body { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitFunctionAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/NumberExprAST.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/NumberExprAST.cs deleted file mode 100644 index bd823cd3..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/NumberExprAST.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace Kaleidoscope.AST -{ - public sealed class NumberExprAST : ExprAST - { - public NumberExprAST(double value) - { - this.Value = value; - this.NodeType = ExprType.NumberExpr; - } - - public double Value { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitNumberExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/PrototypeAST.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/PrototypeAST.cs deleted file mode 100644 index 3d0f6b4a..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/PrototypeAST.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace Kaleidoscope.AST -{ - using System.Collections.Generic; - - public sealed class PrototypeAST : ExprAST - { - public PrototypeAST(string name, List args) - { - this.Name = name; - this.Arguments = args; - this.NodeType = ExprType.PrototypeExpr; - } - - public string Name { get; private set; } - - public List Arguments { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitPrototypeAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/VariableExprAST.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/VariableExprAST.cs deleted file mode 100644 index 41aeb6c2..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/AST/VariableExprAST.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace Kaleidoscope.AST -{ - public sealed class VariableExprAST : ExprAST - { - public VariableExprAST(string name) - { - this.Name = name; - this.NodeType = ExprType.VariableExpr; - } - - public string Name { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitVariableExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/BaseParserListener.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/BaseParserListener.cs deleted file mode 100644 index 6fb50254..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/BaseParserListener.cs +++ /dev/null @@ -1,65 +0,0 @@ -namespace Kaleidoscope -{ - using System; - using System.Collections.Generic; - using System.Reflection; - using AST; - - internal sealed class BaseParserListener - { - private static readonly Type IParserListenerType = typeof(IParserListener); - - private readonly Stack descentStack = new Stack(); - - private readonly Stack ascentStack = new Stack(); - - private readonly IParserListener listener; - - public BaseParserListener(IParserListener listener) - { - this.listener = listener; - } - - public void EnterRule(string ruleName) - { - this.descentStack.Push(ruleName); - } - - public void ExitRule(ExprAST argument) - { - string ruleName = this.descentStack.Pop(); - this.ascentStack.Push(new ASTContext(IParserListenerType.GetMethod("Exit" + ruleName), this.listener, - argument)); - this.ascentStack.Push(new ASTContext(IParserListenerType.GetMethod("Enter" + ruleName), this.listener, - argument)); - } - - public void Listen() - { - if (this.listener != null) - { - while (this.ascentStack.Count != 0) - { - var context = this.ascentStack.Pop(); - context.MethodInfo.Invoke(context.Instance, new object[] {context.Argument}); - } - } - } - - private sealed class ASTContext - { - public ASTContext(MethodInfo methodInfo, object instance, ExprAST argument) - { - this.MethodInfo = methodInfo; - this.Instance = instance; - this.Argument = argument; - } - - public MethodInfo MethodInfo { get; private set; } - - public ExprAST Argument { get; set; } - - public object Instance { get; private set; } - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/ILexer.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/ILexer.cs deleted file mode 100644 index aca5d18f..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/ILexer.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Kaleidoscope -{ - public interface ILexer - { - int CurrentToken { get; } - - string GetLastIdentifier(); - - double GetLastNumber(); - - int GetTokPrecedence(); - - int GetNextToken(); - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/IParser.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/IParser.cs deleted file mode 100644 index adf84705..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/IParser.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Kaleidoscope -{ - public interface IParser - { - void HandleDefinition(); - - void HandleExtern(); - - void HandleTopLevelExpression(); - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/IParserListener.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/IParserListener.cs deleted file mode 100644 index fbc91f82..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/IParserListener.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace Kaleidoscope -{ - using AST; - - public interface IParserListener - { - void EnterHandleDefinition(FunctionAST data); - - void ExitHandleDefinition(FunctionAST data); - - void EnterHandleExtern(PrototypeAST data); - - void ExitHandleExtern(PrototypeAST data); - - void EnterHandleTopLevelExpression(FunctionAST data); - - void ExitHandleTopLevelExpression(FunctionAST data); - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Kaleidoscope.csproj b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Kaleidoscope.csproj deleted file mode 100644 index 0c3ad700..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Kaleidoscope.csproj +++ /dev/null @@ -1,6 +0,0 @@ - - - - net8.0 - - diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Lexer.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Lexer.cs deleted file mode 100644 index f73d0612..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Lexer.cs +++ /dev/null @@ -1,133 +0,0 @@ -namespace Kaleidoscope -{ - using System; - using System.Collections.Generic; - using System.Globalization; - using System.IO; - using System.Text; - - public sealed class Lexer : ILexer - { - private const int EOF = -1; - - private readonly TextReader reader; - - private readonly StringBuilder identifierBuilder = new StringBuilder(); - - private readonly StringBuilder numberBuilder = new StringBuilder(); - - private readonly Dictionary binopPrecedence; - - private int c = ' '; - - private string identifier; - - private double numVal; - - public Lexer(TextReader reader, Dictionary binOpPrecedence) - { - this.reader = reader; - this.binopPrecedence = binOpPrecedence; - } - - public int CurrentToken { get; private set; } - - public string GetLastIdentifier() - { - return this.identifier; - } - - public double GetLastNumber() - { - return this.numVal; - } - - public int GetTokPrecedence() - { - // Make sure it's a declared binop. - if (this.binopPrecedence.TryGetValue((char)this.CurrentToken, out var tokPrec)) - { - return tokPrec; - } - - return -1; - } - - public int GetNextToken() - { - // Skip any whitespace. - while (char.IsWhiteSpace((char)c)) - { - c = this.reader.Read(); - } - - if (char.IsLetter((char)c)) // identifier: [a-zA-Z][a-zA-Z0-9]* - { - this.identifierBuilder.Append((char)c); - while (char.IsLetterOrDigit((char)(c = this.reader.Read()))) - { - this.identifierBuilder.Append((char)c); - } - - this.identifier = this.identifierBuilder.ToString(); - this.identifierBuilder.Clear(); - - if (string.Equals(identifier, "def", StringComparison.Ordinal)) - { - this.CurrentToken = (int)Token.DEF; - } - else if (string.Equals(identifier, "extern", StringComparison.Ordinal)) - { - this.CurrentToken = (int)Token.EXTERN; - } - else - { - this.CurrentToken = (int)Token.IDENTIFIER; - } - - return this.CurrentToken; - } - - // Number: [0-9.]+ - if (char.IsDigit((char)c) || c == '.') - { - do - { - this.numberBuilder.Append((char)c); - c = this.reader.Read(); - } while (char.IsDigit((char)c) || c == '.'); - - this.numVal = double.Parse(this.numberBuilder.ToString()); - this.numberBuilder.Clear(); - this.CurrentToken = (int)Token.NUMBER; - - return this.CurrentToken; - } - - if (c == '#') - { - // Comment until end of line. - do - { - c = this.reader.Read(); - } while (c != EOF && c != '\n' && c != '\r'); - - if (c != EOF) - { - return this.GetNextToken(); - } - } - - // Check for end of file. Don't eat the EOF. - if (c == EOF) - { - this.CurrentToken = c; - return (int)Token.EOF; - } - - this.CurrentToken = c; - c = this.reader.Read(); - return this.c; - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Parser.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Parser.cs deleted file mode 100644 index bd17834f..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Parser.cs +++ /dev/null @@ -1,313 +0,0 @@ -namespace Kaleidoscope -{ - using System; - using System.Collections.Generic; - using AST; - - public sealed class Parser : IParser - { - private readonly Lexer scanner; - - private readonly BaseParserListener baseListener; - - public Parser(Lexer scanner, IParserListener listener) - { - this.scanner = scanner; - this.baseListener = new BaseParserListener(listener); - } - - public void HandleDefinition() - { - this.baseListener.EnterRule("HandleDefinition"); - - var functionAST = this.ParseDefinition(); - - baseListener.ExitRule(functionAST); - - if (functionAST != null) - { - this.baseListener.Listen(); - } - else - { - // Skip token for error recovery. - this.scanner.GetNextToken(); - } - } - - public void HandleExtern() - { - this.baseListener.EnterRule("HandleExtern"); - - var prototypeAST = this.ParseExtern(); - - this.baseListener.ExitRule(prototypeAST); - - if (prototypeAST != null) - { - this.baseListener.Listen(); - } - else - { - // Skip token for error recovery. - this.scanner.GetNextToken(); - } - } - - public void HandleTopLevelExpression() - { - // Evaluate a top-level expression into an anonymous function. - this.baseListener.EnterRule("HandleTopLevelExpression"); - - var functionAST = this.ParseTopLevelExpr(); - - this.baseListener.ExitRule(functionAST); - - if (functionAST != null) - { - this.baseListener.Listen(); - } - else - { - // Skip token for error recovery. - this.scanner.GetNextToken(); - } - } - - // identifierexpr - // ::= identifier - // ::= identifier '(' expression* ')' - private ExprAST ParseIdentifierExpr() - { - string idName = this.scanner.GetLastIdentifier(); - - this.scanner.GetNextToken(); // eat identifier. - - if (this.scanner.CurrentToken != '(') // Simple variable ref. - { - return new VariableExprAST(idName); - } - - // Call. - this.scanner.GetNextToken(); // eat ( - List args = new List(); - - if (this.scanner.CurrentToken != ')') - { - while (true) - { - ExprAST arg = this.ParseExpression(); - if (arg == null) - { - return null; - } - - args.Add(arg); - - if (this.scanner.CurrentToken == ')') - { - break; - } - - if (this.scanner.CurrentToken != ',') - { - Console.WriteLine("Expected ')' or ',' in argument list"); - return null; - } - - this.scanner.GetNextToken(); - } - } - - // Eat the ')'. - this.scanner.GetNextToken(); - - return new CallExprAST(idName, args); - } - - // numberexpr ::= number - private ExprAST ParseNumberExpr() - { - ExprAST result = new NumberExprAST(this.scanner.GetLastNumber()); - this.scanner.GetNextToken(); - return result; - } - - // parenexpr ::= '(' expression ')' - private ExprAST ParseParenExpr() - { - this.scanner.GetNextToken(); // eat (. - ExprAST v = this.ParseExpression(); - if (v == null) - { - return null; - } - - if (this.scanner.CurrentToken != ')') - { - Console.WriteLine("expected ')'"); - return null; - } - - this.scanner.GetNextToken(); // eat ). - - return v; - } - - // primary - // ::= identifierexpr - // ::= numberexpr - // ::= parenexpr - private ExprAST ParsePrimary() - { - switch (this.scanner.CurrentToken) - { - case (int) Token.IDENTIFIER: - return this.ParseIdentifierExpr(); - case (int) Token.NUMBER: - return this.ParseNumberExpr(); - case '(': - return this.ParseParenExpr(); - default: - Console.WriteLine("unknown token when expecting an expression"); - return null; - } - } - - // binoprhs - // ::= ('+' primary)* - private ExprAST ParseBinOpRHS(int exprPrec, ExprAST lhs) - { - // If this is a binop, find its precedence. - while (true) - { - int tokPrec = this.scanner.GetTokPrecedence(); - - // If this is a binop that binds at least as tightly as the current binop, - // consume it, otherwise we are done. - if (tokPrec < exprPrec) - { - return lhs; - } - - // Okay, we know this is a binop. - int binOp = this.scanner.CurrentToken; - this.scanner.GetNextToken(); // eat binop - - // Parse the primary expression after the binary operator. - ExprAST rhs = this.ParsePrimary(); - if (rhs == null) - { - return null; - } - - // If BinOp binds less tightly with RHS than the operator after RHS, let - // the pending operator take RHS as its LHS. - int nextPrec = this.scanner.GetTokPrecedence(); - if (tokPrec < nextPrec) - { - rhs = this.ParseBinOpRHS(tokPrec + 1, rhs); - if (rhs == null) - { - return null; - } - } - - // Merge LHS/RHS. - lhs = new BinaryExprAST((char) binOp, lhs, rhs); - } - } - - // expression - // ::= primary binoprhs - // - private ExprAST ParseExpression() - { - ExprAST lhs = this.ParsePrimary(); - if (lhs == null) - { - return null; - } - - return this.ParseBinOpRHS(0, lhs); - } - - // prototype - // ::= id '(' id* ')' - private PrototypeAST ParsePrototype() - { - if (this.scanner.CurrentToken != (int) Token.IDENTIFIER) - { - Console.WriteLine("Expected function name in prototype"); - return null; - } - - string fnName = this.scanner.GetLastIdentifier(); - - this.scanner.GetNextToken(); - - if (this.scanner.CurrentToken != '(') - { - Console.WriteLine("Expected '(' in prototype"); - return null; - } - - List argNames = new List(); - while (this.scanner.GetNextToken() == (int) Token.IDENTIFIER) - { - argNames.Add(this.scanner.GetLastIdentifier()); - } - - if (this.scanner.CurrentToken != ')') - { - Console.WriteLine("Expected ')' in prototype"); - return null; - } - - this.scanner.GetNextToken(); // eat ')'. - - return new PrototypeAST(fnName, argNames); - } - - // definition ::= 'def' prototype expression - private FunctionAST ParseDefinition() - { - this.scanner.GetNextToken(); // eat def. - PrototypeAST proto = this.ParsePrototype(); - - if (proto == null) - { - return null; - } - - ExprAST body = this.ParseExpression(); - if (body == null) - { - return null; - } - - return new FunctionAST(proto, body); - } - - /// toplevelexpr ::= expression - private FunctionAST ParseTopLevelExpr() - { - ExprAST e = this.ParseExpression(); - if (e == null) - { - return null; - } - - // Make an anonymous proto. - PrototypeAST proto = new PrototypeAST(string.Empty, new List()); - return new FunctionAST(proto, e); - } - - /// external ::= 'extern' prototype - private PrototypeAST ParseExtern() - { - this.scanner.GetNextToken(); // eat extern. - return this.ParsePrototype(); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Token.cs b/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Token.cs deleted file mode 100644 index a4f84c23..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/Kaleidoscope/Token.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Kaleidoscope -{ - public enum Token - { - EOF = -1, - DEF = -2, - EXTERN = -3, - IDENTIFIER = -4, - NUMBER = -5, - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/CodeGenParserListener.cs b/samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/CodeGenParserListener.cs deleted file mode 100644 index d1cd661f..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/CodeGenParserListener.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using LLVMSharp; - -namespace KaleidoscopeLLVM -{ - using Kaleidoscope; - using Kaleidoscope.AST; - - internal sealed class CodeGenParserListener : IParserListener - { - private readonly CodeGenVisitor visitor; - - private readonly LLVMExecutionEngineRef ee; - - private readonly LLVMPassManagerRef passManager; - - public CodeGenParserListener(LLVMExecutionEngineRef ee, LLVMPassManagerRef passManager, CodeGenVisitor visitor) - { - this.visitor = visitor; - this.ee = ee; - this.passManager = passManager; - } - - public void EnterHandleDefinition(FunctionAST data) - { - } - - public void ExitHandleDefinition(FunctionAST data) - { - this.visitor.Visit(data); - var function = this.visitor.ResultStack.Pop(); - // LLVM.DumpValue(function); - - LLVM.RunFunctionPassManager(this.passManager, function); - // LLVM.DumpValue(function); // Dump the function for exposition purposes. - } - - public void EnterHandleExtern(PrototypeAST data) - { - } - - public void ExitHandleExtern(PrototypeAST data) - { - this.visitor.Visit(data); - // LLVM.DumpValue(this.visitor.ResultStack.Pop()); - } - - public void EnterHandleTopLevelExpression(FunctionAST data) - { - } - - public void ExitHandleTopLevelExpression(FunctionAST data) - { - this.visitor.Visit(data); - var anonymousFunction = this.visitor.ResultStack.Pop(); -// LLVM.DumpValue(anonymousFunction); // Dump the function for exposition purposes. - var dFunc = (Program.D) Marshal.GetDelegateForFunctionPointer( - LLVM.GetPointerToGlobal(this.ee, anonymousFunction), typeof(Program.D)); - LLVM.RunFunctionPassManager(this.passManager, anonymousFunction); - -// LLVM.DumpValue(anonymousFunction); // Dump the function for exposition purposes. - Console.WriteLine("Evaluated to " + dFunc()); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/CodeGenVisitor.cs b/samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/CodeGenVisitor.cs deleted file mode 100644 index 275b1796..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/CodeGenVisitor.cs +++ /dev/null @@ -1,199 +0,0 @@ -namespace KaleidoscopeLLVM -{ - using System; - using System.Collections.Generic; - using Kaleidoscope.AST; - using LLVMSharp; - - internal sealed class CodeGenVisitor : ExprVisitor - { - private static readonly LLVMBool LLVMBoolFalse = new LLVMBool(0); - - private static readonly LLVMValueRef NullValue = new LLVMValueRef(IntPtr.Zero); - - private readonly LLVMModuleRef module; - - private readonly LLVMBuilderRef builder; - - private readonly Dictionary namedValues = new Dictionary(); - - private readonly Stack valueStack = new Stack(); - - public CodeGenVisitor(LLVMModuleRef module, LLVMBuilderRef builder) - { - this.module = module; - this.builder = builder; - } - - public Stack ResultStack => valueStack; - - public void ClearResultStack() - { - this.valueStack.Clear(); - } - - protected override ExprAST VisitNumberExprAST(NumberExprAST node) - { - this.valueStack.Push(LLVM.ConstReal(LLVM.DoubleType(), node.Value)); - return node; - } - - protected override ExprAST VisitVariableExprAST(VariableExprAST node) - { - // Look this variable up in the function. - if (this.namedValues.TryGetValue(node.Name, out var value)) - { - this.valueStack.Push(value); - } - else - { - throw new Exception($"Unknown variable name {node.Name}"); - } - - return node; - } - - protected override ExprAST VisitBinaryExprAST(BinaryExprAST node) - { - this.Visit(node.Lhs); - this.Visit(node.Rhs); - - LLVMValueRef r = this.valueStack.Pop(); - LLVMValueRef l = this.valueStack.Pop(); - - LLVMValueRef n; - - switch (node.NodeType) - { - case ExprType.AddExpr: - n = LLVM.BuildFAdd(this.builder, l, r, "addtmp"); - break; - case ExprType.SubtractExpr: - n = LLVM.BuildFSub(this.builder, l, r, "subtmp"); - break; - case ExprType.MultiplyExpr: - n = LLVM.BuildFMul(this.builder, l, r, "multmp"); - break; - case ExprType.LessThanExpr: - // Convert bool 0/1 to double 0.0 or 1.0 - n = LLVM.BuildUIToFP(this.builder, - LLVM.BuildFCmp(this.builder, LLVMRealPredicate.LLVMRealULT, l, r, "cmptmp"), LLVM.DoubleType(), - "booltmp"); - break; - default: - throw new Exception("invalid binary operator"); - } - - this.valueStack.Push(n); - return node; - } - - protected override ExprAST VisitCallExprAST(CallExprAST node) - { - var calleeF = LLVM.GetNamedFunction(this.module, node.Callee); - if (calleeF.Pointer == IntPtr.Zero) - { - throw new Exception($"Unknown function referenced {node.Callee}"); - } - - if (LLVM.CountParams(calleeF) != node.Arguments.Count) - { - throw new Exception("Incorrect # arguments passed"); - } - - var argumentCount = (uint) node.Arguments.Count; - var argsV = new LLVMValueRef[Math.Max(argumentCount, 1)]; - for (int i = 0; i < argumentCount; ++i) - { - this.Visit(node.Arguments[i]); - argsV[i] = this.valueStack.Pop(); - } - - this.valueStack.Push(LLVM.BuildCall(this.builder, calleeF, argsV, "calltmp")); - - return node; - } - - protected override ExprAST VisitPrototypeAST(PrototypeAST node) - { - // Make the function type: double(double,double) etc. - var argumentCount = (uint) node.Arguments.Count; - var arguments = new LLVMTypeRef[Math.Max(argumentCount, 0)]; - - var function = LLVM.GetNamedFunction(this.module, node.Name); - - // If F conflicted, there was already something named 'Name'. If it has a - // body, don't allow redefinition or reextern. - if (function.Pointer != IntPtr.Zero) - { - // If F already has a body, reject this. - if (LLVM.CountBasicBlocks(function) != 0) - { - throw new Exception("redefinition of function."); - } - - // If F took a different number of args, reject. - if (LLVM.CountParams(function) != argumentCount) - { - throw new Exception("redefinition of function with different # args"); - } - } - else - { - for (int i = 0; i < argumentCount; ++i) - { - arguments[i] = LLVM.DoubleType(); - } - - function = LLVM.AddFunction(this.module, node.Name, - LLVM.FunctionType(LLVM.DoubleType(), arguments, LLVMBoolFalse)); - LLVM.SetLinkage(function, LLVMLinkage.LLVMExternalLinkage); - } - - for (int i = 0; i < argumentCount; ++i) - { - var argumentName = node.Arguments[i]; - - LLVMValueRef param = LLVM.GetParam(function, (uint) i); - LLVM.SetValueName(param, argumentName); - - this.namedValues[argumentName] = param; - } - - this.valueStack.Push(function); - return node; - } - - protected override ExprAST VisitFunctionAST(FunctionAST node) - { - namedValues.Clear(); - - Visit(node.Proto); - - LLVMValueRef function = valueStack.Pop(); - - // Create a new basic block to start insertion into. - LLVM.PositionBuilderAtEnd(builder, LLVM.AppendBasicBlock(function, "entry")); - - try - { - Visit(node.Body); - } - catch (Exception) - { - LLVM.DeleteFunction(function); - throw; - } - - // Finish off the function. - LLVM.BuildRet(builder, valueStack.Pop()); - - // Validate the generated code, checking for consistency. - LLVM.VerifyFunction(function, LLVMVerifierFailureAction.LLVMPrintMessageAction); - - valueStack.Push(function); - - return node; - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/KaleidoscopeLLVM.csproj b/samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/KaleidoscopeLLVM.csproj deleted file mode 100644 index 3bf1f997..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/KaleidoscopeLLVM.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - net8.0 - - - - - - - - - - diff --git a/samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/Program.cs b/samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/Program.cs deleted file mode 100644 index edfc659c..00000000 --- a/samples/KaleidoscopeTutorial/Chapter4/KaleidoscopeLLVM/Program.cs +++ /dev/null @@ -1,111 +0,0 @@ -namespace KaleidoscopeLLVM -{ - using System; - using System.Collections.Generic; - using Kaleidoscope; - using LLVMSharp; - - public sealed class Program - { - public delegate double D(); - - public static void Main(string[] args) - { - // Make the module, which holds all the code. - LLVMModuleRef module = LLVM.ModuleCreateWithName("my cool jit"); - LLVMBuilderRef builder = LLVM.CreateBuilder(); - LLVM.LinkInMCJIT(); - LLVM.InitializeX86TargetMC(); - LLVM.InitializeX86Target(); - LLVM.InitializeX86TargetInfo(); - LLVM.InitializeX86AsmParser(); - LLVM.InitializeX86AsmPrinter(); - - if (LLVM.CreateExecutionEngineForModule(out var engine, module, out var errorMessage).Value == 1) - { - Console.WriteLine(errorMessage); - // LLVM.DisposeMessage(errorMessage); - return; - } - - // Create a function pass manager for this engine - LLVMPassManagerRef passManager = LLVM.CreateFunctionPassManagerForModule(module); - - // Set up the optimizer pipeline. Start with registering info about how the - // target lays out data structures. - // LLVM.DisposeTargetData(LLVM.GetExecutionEngineTargetData(engine)); - - // Provide basic AliasAnalysis support for GVN. - LLVM.AddBasicAliasAnalysisPass(passManager); - - // Promote allocas to registers. - LLVM.AddPromoteMemoryToRegisterPass(passManager); - - // Do simple "peephole" optimizations and bit-twiddling optzns. - LLVM.AddInstructionCombiningPass(passManager); - - // Reassociate expressions. - LLVM.AddReassociatePass(passManager); - - // Eliminate Common SubExpressions. - LLVM.AddGVNPass(passManager); - - // Simplify the control flow graph (deleting unreachable blocks, etc). - LLVM.AddCFGSimplificationPass(passManager); - - LLVM.InitializeFunctionPassManager(passManager); - - var codeGenlistener = new CodeGenParserListener(engine, passManager, new CodeGenVisitor(module, builder)); - - // Install standard binary operators. - // 1 is lowest precedence. - var binopPrecedence = new Dictionary - { - ['<'] = 10, - ['+'] = 20, - ['-'] = 20, - ['*'] = 40 - }; - // highest. - - var scanner = new Lexer(Console.In, binopPrecedence); - var parser = new Parser(scanner, codeGenlistener); - - // Prime the first token. - Console.Write("ready> "); - scanner.GetNextToken(); - - // Run the main "interpreter loop" now. - MainLoop(scanner, parser); - - // Print out all of the generated code. - LLVM.DumpModule(module); - } - - private static void MainLoop(ILexer lexer, IParser parser) - { - // top ::= definition | external | expression | ';' - while (true) - { - Console.Write("ready> "); - switch (lexer.CurrentToken) - { - case (int) Token.EOF: - return; - case ';': - lexer.GetNextToken(); - break; - case (int) Token.DEF: - parser.HandleDefinition(); - break; - case (int) Token.EXTERN: - parser.HandleExtern(); - break; - default: - parser.HandleTopLevelExpression(); - break; - } - } - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope.sln b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope.sln deleted file mode 100644 index eae9808a..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope.sln +++ /dev/null @@ -1,37 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.31101.0 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kaleidoscope", "Kaleidoscope\Kaleidoscope.csproj", "{CD23CB2E-951B-4FAD-A4CD-4048731DBA31}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KaleidoscopeLLVM", "KaleidoscopeLLVM\KaleidoscopeLLVM.csproj", "{C1147A13-4174-410A-A891-335D858703B7}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Debug|x64 = Debug|x64 - Release|Any CPU = Release|Any CPU - Release|x64 = Release|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Debug|x64.ActiveCfg = Debug|x64 - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Debug|x64.Build.0 = Debug|x64 - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Release|Any CPU.Build.0 = Release|Any CPU - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Release|x64.ActiveCfg = Release|x64 - {CD23CB2E-951B-4FAD-A4CD-4048731DBA31}.Release|x64.Build.0 = Release|x64 - {C1147A13-4174-410A-A891-335D858703B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C1147A13-4174-410A-A891-335D858703B7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C1147A13-4174-410A-A891-335D858703B7}.Debug|x64.ActiveCfg = Debug|x64 - {C1147A13-4174-410A-A891-335D858703B7}.Debug|x64.Build.0 = Debug|x64 - {C1147A13-4174-410A-A891-335D858703B7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C1147A13-4174-410A-A891-335D858703B7}.Release|Any CPU.Build.0 = Release|Any CPU - {C1147A13-4174-410A-A891-335D858703B7}.Release|x64.ActiveCfg = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/BinaryExprAST.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/BinaryExprAST.cs deleted file mode 100644 index 1143f724..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/BinaryExprAST.cs +++ /dev/null @@ -1,42 +0,0 @@ -namespace Kaleidoscope.AST -{ - using System; - - public sealed class BinaryExprAST : ExprAST - { - public BinaryExprAST(char op, ExprAST lhs, ExprAST rhs) - { - switch (op) - { - case '+': - this.NodeType = ExprType.AddExpr; - break; - case '-': - this.NodeType = ExprType.SubtractExpr; - break; - case '*': - this.NodeType = ExprType.MultiplyExpr; - break; - case '<': - this.NodeType = ExprType.LessThanExpr; - break; - default: - throw new ArgumentException("op " + op + " is not a valid operator"); - } - - this.Lhs = lhs; - this.Rhs = rhs; - } - - public ExprAST Lhs { get; private set; } - - public ExprAST Rhs { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitBinaryExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/CallExprAST.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/CallExprAST.cs deleted file mode 100644 index 1a722b27..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/CallExprAST.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace Kaleidoscope.AST -{ - using System.Collections.Generic; - - public sealed class CallExprAST : ExprAST - { - public CallExprAST(string callee, List args) - { - this.Callee = callee; - this.Arguments = args; - this.NodeType = ExprType.CallExpr; - } - - public string Callee { get; private set; } - - public List Arguments { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitCallExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ExprAST.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ExprAST.cs deleted file mode 100644 index 6841cbec..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ExprAST.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Kaleidoscope.AST -{ - public abstract class ExprAST - { - public abstract ExprType NodeType { get; protected set; } - - protected internal virtual ExprAST VisitChildren(ExprVisitor visitor) - { - return visitor.Visit(this); - } - - protected internal virtual ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitExtension(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ExprType.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ExprType.cs deleted file mode 100644 index 5e89158a..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ExprType.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Kaleidoscope.AST -{ - public enum ExprType - { - AddExpr, - SubtractExpr, - MultiplyExpr, - LessThanExpr, - CallExpr, - VariableExpr, - PrototypeExpr, - FunctionExpr, - NumberExpr, - IfExpr, - ForExpr, - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ExprVisitor.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ExprVisitor.cs deleted file mode 100644 index 43b36369..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ExprVisitor.cs +++ /dev/null @@ -1,84 +0,0 @@ -namespace Kaleidoscope.AST -{ - public abstract class ExprVisitor - { - protected ExprVisitor() - { - } - - public virtual ExprAST Visit(ExprAST node) - { - if (node != null) - { - return node.Accept(this); - } - - return null; - } - - protected internal virtual ExprAST VisitExtension(ExprAST node) - { - return node.VisitChildren(this); - } - - protected internal virtual ExprAST VisitBinaryExprAST(BinaryExprAST node) - { - this.Visit(node.Lhs); - this.Visit(node.Rhs); - - return node; - } - - protected internal virtual ExprAST VisitCallExprAST(CallExprAST node) - { - foreach (var argument in node.Arguments) - { - this.Visit(argument); - } - - return node; - } - - protected internal virtual ExprAST VisitFunctionAST(FunctionAST node) - { - this.Visit(node.Proto); - this.Visit(node.Body); - - return node; - } - - protected internal virtual ExprAST VisitVariableExprAST(VariableExprAST node) - { - return node; - } - - protected internal virtual ExprAST VisitPrototypeAST(PrototypeAST node) - { - return node; - } - - protected internal virtual ExprAST VisitNumberExprAST(NumberExprAST node) - { - return node; - } - - protected internal virtual ExprAST VisitIfExprAST(IfExpAST node) - { - this.Visit(node.Condition); - this.Visit(node.Then); - this.Visit(node.Else); - - return node; - } - - protected internal virtual ExprAST VisitForExprAST(ForExprAST node) - { - this.Visit(node.Start); - this.Visit(node.End); - this.Visit(node.Step); - this.Visit(node.Body); - - return node; - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ForExprAST.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ForExprAST.cs deleted file mode 100644 index 1316c221..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/ForExprAST.cs +++ /dev/null @@ -1,32 +0,0 @@ -namespace Kaleidoscope.AST -{ - public sealed class ForExprAST : ExprAST - { - public ForExprAST(string varName, ExprAST start, ExprAST end, ExprAST step, ExprAST body) - { - this.VarName = varName; - this.Start = start; - this.End = end; - this.Step = step; - this.Body = body; - this.NodeType = ExprType.ForExpr; - } - - public string VarName { get; private set; } - - public ExprAST Start { get; private set; } - - public ExprAST End { get; private set; } - - public ExprAST Step { get; private set; } - - public ExprAST Body { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitForExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/FunctionAST.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/FunctionAST.cs deleted file mode 100644 index a5e90405..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/FunctionAST.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace Kaleidoscope.AST -{ - public sealed class FunctionAST : ExprAST - { - public FunctionAST(PrototypeAST proto, ExprAST body) - { - this.Proto = proto; - this.Body = body; - this.NodeType = ExprType.FunctionExpr; - } - - public PrototypeAST Proto { get; private set; } - - public ExprAST Body { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitFunctionAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/IfExpAST.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/IfExpAST.cs deleted file mode 100644 index 12b3acb0..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/IfExpAST.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace Kaleidoscope.AST -{ - public sealed class IfExpAST : ExprAST - { - public IfExpAST(ExprAST condition, ExprAST then, ExprAST @else) - { - this.Condition = condition; - this.Then = then; - this.Else = @else; - this.NodeType = ExprType.IfExpr; - } - - public ExprAST Condition { get; private set; } - - public ExprAST Then { get; private set; } - - public ExprAST Else { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitIfExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/NumberExprAST.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/NumberExprAST.cs deleted file mode 100644 index bd823cd3..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/NumberExprAST.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace Kaleidoscope.AST -{ - public sealed class NumberExprAST : ExprAST - { - public NumberExprAST(double value) - { - this.Value = value; - this.NodeType = ExprType.NumberExpr; - } - - public double Value { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitNumberExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/PrototypeAST.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/PrototypeAST.cs deleted file mode 100644 index 3d0f6b4a..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/PrototypeAST.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace Kaleidoscope.AST -{ - using System.Collections.Generic; - - public sealed class PrototypeAST : ExprAST - { - public PrototypeAST(string name, List args) - { - this.Name = name; - this.Arguments = args; - this.NodeType = ExprType.PrototypeExpr; - } - - public string Name { get; private set; } - - public List Arguments { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitPrototypeAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/VariableExprAST.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/VariableExprAST.cs deleted file mode 100644 index 41aeb6c2..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/AST/VariableExprAST.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace Kaleidoscope.AST -{ - public sealed class VariableExprAST : ExprAST - { - public VariableExprAST(string name) - { - this.Name = name; - this.NodeType = ExprType.VariableExpr; - } - - public string Name { get; private set; } - - public override ExprType NodeType { get; protected set; } - - protected internal override ExprAST Accept(ExprVisitor visitor) - { - return visitor.VisitVariableExprAST(this); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/BaseParserListener.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/BaseParserListener.cs deleted file mode 100644 index 9eb96731..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/BaseParserListener.cs +++ /dev/null @@ -1,63 +0,0 @@ -namespace Kaleidoscope -{ - using System; - using System.Collections.Generic; - using System.Reflection; - using AST; - - internal sealed class BaseParserListener - { - private static readonly Type IParserListenerType = typeof(IParserListener); - - private readonly Stack descentStack = new Stack(); - - private readonly Stack ascentStack = new Stack(); - - private readonly IParserListener listener; - - public BaseParserListener(IParserListener listener) - { - this.listener = listener; - } - - public void EnterRule(string ruleName) - { - this.descentStack.Push(ruleName); - } - - public void ExitRule(ExprAST argument) - { - string ruleName = this.descentStack.Pop(); - this.ascentStack.Push(new ASTContext(IParserListenerType.GetMethod("Exit" + ruleName), this.listener, argument)); - this.ascentStack.Push(new ASTContext(IParserListenerType.GetMethod("Enter" + ruleName), this.listener, argument)); - } - - public void Listen() - { - if (this.listener != null) - { - while (this.ascentStack.Count != 0) - { - var context = this.ascentStack.Pop(); - context.MethodInfo.Invoke(context.Instance, new object[] { context.Argument }); - } - } - } - - private sealed class ASTContext - { - public ASTContext(MethodInfo methodInfo, object instance, ExprAST argument) - { - this.MethodInfo = methodInfo; - this.Instance = instance; - this.Argument = argument; - } - - public MethodInfo MethodInfo { get; private set; } - - public ExprAST Argument { get; set; } - - public object Instance { get; private set; } - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/ILexer.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/ILexer.cs deleted file mode 100644 index aca5d18f..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/ILexer.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Kaleidoscope -{ - public interface ILexer - { - int CurrentToken { get; } - - string GetLastIdentifier(); - - double GetLastNumber(); - - int GetTokPrecedence(); - - int GetNextToken(); - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/IParser.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/IParser.cs deleted file mode 100644 index adf84705..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/IParser.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Kaleidoscope -{ - public interface IParser - { - void HandleDefinition(); - - void HandleExtern(); - - void HandleTopLevelExpression(); - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/IParserListener.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/IParserListener.cs deleted file mode 100644 index fbc91f82..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/IParserListener.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace Kaleidoscope -{ - using AST; - - public interface IParserListener - { - void EnterHandleDefinition(FunctionAST data); - - void ExitHandleDefinition(FunctionAST data); - - void EnterHandleExtern(PrototypeAST data); - - void ExitHandleExtern(PrototypeAST data); - - void EnterHandleTopLevelExpression(FunctionAST data); - - void ExitHandleTopLevelExpression(FunctionAST data); - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Kaleidoscope.csproj b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Kaleidoscope.csproj deleted file mode 100644 index 0c3ad700..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Kaleidoscope.csproj +++ /dev/null @@ -1,6 +0,0 @@ - - - - net8.0 - - diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Lexer.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Lexer.cs deleted file mode 100644 index 820d53de..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Lexer.cs +++ /dev/null @@ -1,154 +0,0 @@ -namespace Kaleidoscope -{ - using System; - using System.Collections.Generic; - using System.Globalization; - using System.IO; - using System.Text; - - public sealed class Lexer : ILexer - { - private const int EOF = -1; - - private readonly TextReader reader; - - private readonly StringBuilder identifierBuilder = new StringBuilder(); - - private readonly StringBuilder numberBuilder = new StringBuilder(); - - private readonly Dictionary binopPrecedence; - - private int c = ' '; - - private string identifier; - - private double numVal; - - public Lexer(TextReader reader, Dictionary binOpPrecedence) - { - this.reader = reader; - this.binopPrecedence = binOpPrecedence; - } - - public int CurrentToken { get; private set; } - - public string GetLastIdentifier() - { - return this.identifier; - } - - public double GetLastNumber() - { - return this.numVal; - } - - public int GetTokPrecedence() - { - // Make sure it's a declared binop. - int tokPrec; - if (this.binopPrecedence.TryGetValue((char)this.CurrentToken, out tokPrec)) - { - return tokPrec; - } - - return -1; - } - - public int GetNextToken() - { - // Skip any whitespace. - while (char.IsWhiteSpace((char)c)) - { - c = this.reader.Read(); - } - - if (char.IsLetter((char)c)) // identifier: [a-zA-Z][a-zA-Z0-9]* - { - this.identifierBuilder.Append((char)c); - while (char.IsLetterOrDigit((char)(c = this.reader.Read()))) - { - this.identifierBuilder.Append((char)c); - } - - this.identifier = this.identifierBuilder.ToString(); - this.identifierBuilder.Clear(); - - if (string.Equals(identifier, "def", StringComparison.Ordinal)) - { - this.CurrentToken = (int)Token.DEF; - } - else if (string.Equals(identifier, "extern", StringComparison.Ordinal)) - { - this.CurrentToken = (int)Token.EXTERN; - } - else if (string.Equals(identifier, "if", StringComparison.Ordinal)) - { - this.CurrentToken = (int)Token.IF; - } - else if (string.Equals(identifier, "then", StringComparison.Ordinal)) - { - this.CurrentToken = (int)Token.THEN; - } - else if (string.Equals(identifier, "else", StringComparison.Ordinal)) - { - this.CurrentToken = (int)Token.ELSE; - } - else if (string.Equals(identifier, "for", StringComparison.Ordinal)) - { - this.CurrentToken = (int)Token.FOR; - } - else if (string.Equals(identifier, "in", StringComparison.Ordinal)) - { - this.CurrentToken = (int)Token.IN; - } - else - { - this.CurrentToken = (int)Token.IDENTIFIER; - } - - return this.CurrentToken; - } - - // Number: [0-9.]+ - if (char.IsDigit((char)c) || c == '.') - { - do - { - this.numberBuilder.Append((char)c); - c = this.reader.Read(); - } while (char.IsDigit((char)c) || c == '.'); - - this.numVal = double.Parse(this.numberBuilder.ToString()); - this.numberBuilder.Clear(); - this.CurrentToken = (int)Token.NUMBER; - - return this.CurrentToken; - } - - if (c == '#') - { - // Comment until end of line. - do - { - c = this.reader.Read(); - } while (c != EOF && c != '\n' && c != '\r'); - - if (c != EOF) - { - return this.GetNextToken(); - } - } - - // Check for end of file. Don't eat the EOF. - if (c == EOF) - { - this.CurrentToken = c; - return (int)Token.EOF; - } - - this.CurrentToken = c; - c = this.reader.Read(); - return this.c; - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Parser.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Parser.cs deleted file mode 100644 index f7900a62..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Parser.cs +++ /dev/null @@ -1,431 +0,0 @@ -using System.Diagnostics; - -namespace Kaleidoscope -{ - using System; - using System.Collections.Generic; - using AST; - - public sealed class Parser : IParser - { - private readonly Lexer scanner; - - private readonly BaseParserListener baseListener; - - public Parser(Lexer scanner, IParserListener listener) - { - this.scanner = scanner; - this.baseListener = new BaseParserListener(listener); - } - - public void HandleDefinition() - { - this.baseListener.EnterRule("HandleDefinition"); - - var functionAST = this.ParseDefinition(); - - baseListener.ExitRule(functionAST); - - if (functionAST != null) - { - this.baseListener.Listen(); - } - else - { - // Skip token for error recovery. - this.scanner.GetNextToken(); - } - } - - public void HandleExtern() - { - this.baseListener.EnterRule("HandleExtern"); - - var prototypeAST = this.ParseExtern(); - - this.baseListener.ExitRule(prototypeAST); - - if (prototypeAST != null) - { - this.baseListener.Listen(); - } - else - { - // Skip token for error recovery. - this.scanner.GetNextToken(); - } - } - - public void HandleTopLevelExpression() - { - // Evaluate a top-level expression into an anonymous function. - this.baseListener.EnterRule("HandleTopLevelExpression"); - - var functionAST = this.ParseTopLevelExpr(); - - this.baseListener.ExitRule(functionAST); - - if (functionAST != null) - { - this.baseListener.Listen(); - } - else - { - // Skip token for error recovery. - this.scanner.GetNextToken(); - } - } - - // identifierexpr - // ::= identifier - // ::= identifier '(' expression* ')' - private ExprAST ParseIdentifierExpr() - { - string idName = this.scanner.GetLastIdentifier(); - - this.scanner.GetNextToken(); // eat identifier. - - if (this.scanner.CurrentToken != '(') // Simple variable ref. - { - return new VariableExprAST(idName); - } - - // Call. - this.scanner.GetNextToken(); // eat ( - List args = new List(); - - if (this.scanner.CurrentToken != ')') - { - while (true) - { - ExprAST arg = this.ParseExpression(); - if (arg == null) - { - return null; - } - - args.Add(arg); - - if (this.scanner.CurrentToken == ')') - { - break; - } - - if (this.scanner.CurrentToken != ',') - { - Console.WriteLine("Expected ')' or ',' in argument list"); - return null; - } - - this.scanner.GetNextToken(); - } - } - - // Eat the ')'. - this.scanner.GetNextToken(); - - return new CallExprAST(idName, args); - } - - // numberexpr ::= number - private ExprAST ParseNumberExpr() - { - ExprAST result = new NumberExprAST(this.scanner.GetLastNumber()); - this.scanner.GetNextToken(); - return result; - } - - // parenexpr ::= '(' expression ')' - private ExprAST ParseParenExpr() - { - this.scanner.GetNextToken(); // eat (. - ExprAST v = this.ParseExpression(); - if (v == null) - { - return null; - } - - if (this.scanner.CurrentToken != ')') - { - Console.WriteLine("expected ')'"); - return null; - } - - this.scanner.GetNextToken(); // eat ). - - return v; - } - - // ifexpr ::= 'if' expression 'then' expression 'else' expression - public ExprAST ParseIfExpr() - { - this.scanner.GetNextToken(); // eat the if. - - // condition - ExprAST cond = this.ParseExpression(); - if (cond == null) - { - return null; - } - - if (this.scanner.CurrentToken != (int)Token.THEN) - { - Console.WriteLine("expected then"); - } - - this.scanner.GetNextToken(); // eat the then - - ExprAST then = this.ParseExpression(); - if (then == null) - { - return null; - } - - if (this.scanner.CurrentToken != (int)Token.ELSE) - { - Console.WriteLine("expected else"); - return null; - } - - this.scanner.GetNextToken(); - - ExprAST @else = this.ParseExpression(); - if (@else == null) - { - return null; - } - - return new IfExpAST(cond, then, @else); - } - - // forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression - public ExprAST ParseForExpr() - { - this.scanner.GetNextToken(); // eat the for. - - if (this.scanner.CurrentToken != (int)Token.IDENTIFIER) - { - Console.WriteLine("expected identifier after for"); - return null; - } - - string idName = this.scanner.GetLastIdentifier(); - this.scanner.GetNextToken(); // eat identifier. - - if (this.scanner.CurrentToken != '=') - { - Console.WriteLine("expected '=' after for"); - return null; - } - - this.scanner.GetNextToken(); // eat '='. - - ExprAST start = this.ParseExpression(); - if (start == null) - { - return null; - } - - if (this.scanner.CurrentToken != ',') - { - Console.WriteLine("expected ',' after for start value"); - return null; - } - - this.scanner.GetNextToken(); - - ExprAST end = this.ParseExpression(); - if (end == null) - { - return null; - } - - // The step value is optional; - ExprAST step = null; - if (this.scanner.CurrentToken == ',') - { - this.scanner.GetNextToken(); - step = this.ParseExpression(); - if (step == null) - { - return null; - } - } - - if (this.scanner.CurrentToken != (int)Token.IN) - { - Console.WriteLine("expected 'in' after for"); - return null; - } - - this.scanner.GetNextToken(); - ExprAST body = this.ParseExpression(); - if (body == null) - { - return null; - } - - return new ForExprAST(idName, start, end, step, body); - } - - // primary - // ::= identifierexpr - // ::= numberexpr - // ::= parenexpr - private ExprAST ParsePrimary() - { - switch (this.scanner.CurrentToken) - { - case (int)Token.IDENTIFIER: - return this.ParseIdentifierExpr(); - case (int)Token.NUMBER: - return this.ParseNumberExpr(); - case '(': - return this.ParseParenExpr(); - case (int)Token.IF: - return this.ParseIfExpr(); - case (int)Token.FOR: - return this.ParseForExpr(); - default: - Console.WriteLine("unknown token when expecting an expression"); - return null; - } - } - - // binoprhs - // ::= ('+' primary)* - private ExprAST ParseBinOpRHS(int exprPrec, ExprAST lhs) - { - // If this is a binop, find its precedence. - while (true) - { - int tokPrec = this.scanner.GetTokPrecedence(); - - // If this is a binop that binds at least as tightly as the current binop, - // consume it, otherwise we are done. - if (tokPrec < exprPrec) - { - return lhs; - } - - // Okay, we know this is a binop. - int binOp = this.scanner.CurrentToken; - this.scanner.GetNextToken(); // eat binop - - // Parse the primary expression after the binary operator. - ExprAST rhs = this.ParsePrimary(); - if (rhs == null) - { - return null; - } - - // If BinOp binds less tightly with RHS than the operator after RHS, let - // the pending operator take RHS as its LHS. - int nextPrec = this.scanner.GetTokPrecedence(); - if (tokPrec < nextPrec) - { - rhs = this.ParseBinOpRHS(tokPrec + 1, rhs); - if (rhs == null) - { - return null; - } - } - - // Merge LHS/RHS. - lhs = new BinaryExprAST((char)binOp, lhs, rhs); - } - } - - // expression - // ::= primary binoprhs - // - private ExprAST ParseExpression() - { - ExprAST lhs = this.ParsePrimary(); - if (lhs == null) - { - return null; - } - - return this.ParseBinOpRHS(0, lhs); - } - - // prototype - // ::= id '(' id* ')' - private PrototypeAST ParsePrototype() - { - if (this.scanner.CurrentToken != (int)Token.IDENTIFIER) - { - Console.WriteLine("Expected function name in prototype"); - return null; - } - - string fnName = this.scanner.GetLastIdentifier(); - - this.scanner.GetNextToken(); - - if (this.scanner.CurrentToken != '(') - { - Console.WriteLine("Expected '(' in prototype"); - return null; - } - - List argNames = new List(); - while (this.scanner.GetNextToken() == (int)Token.IDENTIFIER) - { - argNames.Add(this.scanner.GetLastIdentifier()); - } - - if (this.scanner.CurrentToken != ')') - { - Console.WriteLine("Expected ')' in prototype"); - return null; - } - - this.scanner.GetNextToken(); // eat ')'. - - return new PrototypeAST(fnName, argNames); - } - - // definition ::= 'def' prototype expression - private FunctionAST ParseDefinition() - { - this.scanner.GetNextToken(); // eat def. - PrototypeAST proto = this.ParsePrototype(); - - if (proto == null) - { - return null; - } - - ExprAST body = this.ParseExpression(); - if (body == null) - { - return null; - } - - return new FunctionAST(proto, body); - } - - /// toplevelexpr ::= expression - private FunctionAST ParseTopLevelExpr() - { - ExprAST e = this.ParseExpression(); - if (e == null) - { - return null; - } - - // Make an anonymous proto. - PrototypeAST proto = new PrototypeAST(string.Empty, new List()); - return new FunctionAST(proto, e); - } - - /// external ::= 'extern' prototype - private PrototypeAST ParseExtern() - { - this.scanner.GetNextToken(); // eat extern. - return this.ParsePrototype(); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Token.cs b/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Token.cs deleted file mode 100644 index 9d9443ed..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/Kaleidoscope/Token.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace Kaleidoscope -{ - public enum Token - { - EOF = -1, - DEF = -2, - EXTERN = -3, - IDENTIFIER = -4, - NUMBER = -5, - IF = -6, - THEN = -7, - ELSE = -8, - FOR = -9, - IN = -10 - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/CodeGenParserListener.cs b/samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/CodeGenParserListener.cs deleted file mode 100644 index 8648340c..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/CodeGenParserListener.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using LLVMSharp; - -namespace KaleidoscopeLLVM -{ - using Kaleidoscope; - using Kaleidoscope.AST; - - internal sealed class CodeGenParserListener : IParserListener - { - private readonly CodeGenVisitor visitor; - - private readonly LLVMExecutionEngineRef ee; - - private readonly LLVMPassManagerRef passManager; - - public CodeGenParserListener(LLVMExecutionEngineRef ee, LLVMPassManagerRef passManager, CodeGenVisitor visitor) - { - this.visitor = visitor; - this.ee = ee; - this.passManager = passManager; - } - - public void EnterHandleDefinition(FunctionAST data) - { - } - - public void ExitHandleDefinition(FunctionAST data) - { - this.visitor.Visit(data); - var function = this.visitor.ResultStack.Pop(); - LLVM.DumpValue(function); - - LLVM.RunFunctionPassManager(this.passManager, function); - LLVM.DumpValue(function); // Dump the function for exposition purposes. - } - - public void EnterHandleExtern(PrototypeAST data) - { - } - - public void ExitHandleExtern(PrototypeAST data) - { - this.visitor.Visit(data); - LLVM.DumpValue(this.visitor.ResultStack.Pop()); - } - - public void EnterHandleTopLevelExpression(FunctionAST data) - { - } - - public void ExitHandleTopLevelExpression(FunctionAST data) - { - this.visitor.Visit(data); - var anonymousFunction = this.visitor.ResultStack.Pop(); - LLVM.DumpValue(anonymousFunction); // Dump the function for exposition purposes. - var dFunc = (Program.D)Marshal.GetDelegateForFunctionPointer(LLVM.GetPointerToGlobal(this.ee, anonymousFunction), typeof(Program.D)); - LLVM.RunFunctionPassManager(this.passManager, anonymousFunction); - - LLVM.DumpValue(anonymousFunction); // Dump the function for exposition purposes. - Console.WriteLine("Evaluated to " + dFunc()); - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/CodeGenVisitor.cs b/samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/CodeGenVisitor.cs deleted file mode 100644 index 46dea6e0..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/CodeGenVisitor.cs +++ /dev/null @@ -1,350 +0,0 @@ -namespace KaleidoscopeLLVM -{ - using System; - using System.Collections.Generic; - using Kaleidoscope.AST; - using LLVMSharp; - - internal sealed class CodeGenVisitor : ExprVisitor - { - private static readonly LLVMBool LLVMBoolFalse = new LLVMBool(0); - - private static readonly LLVMValueRef NullValue = new LLVMValueRef(IntPtr.Zero); - - private readonly LLVMModuleRef module; - - private readonly LLVMBuilderRef builder; - - private readonly Dictionary namedValues = new Dictionary(); - - private readonly Stack valueStack = new Stack(); - - public CodeGenVisitor(LLVMModuleRef module, LLVMBuilderRef builder) - { - this.module = module; - this.builder = builder; - } - - public Stack ResultStack { get { return valueStack; } } - - public void ClearResultStack() - { - this.valueStack.Clear(); - } - - protected override ExprAST VisitNumberExprAST(NumberExprAST node) - { - this.valueStack.Push(LLVM.ConstReal(LLVM.DoubleType(), node.Value)); - return node; - } - - protected override ExprAST VisitVariableExprAST(VariableExprAST node) - { - LLVMValueRef value; - - // Look this variable up in the function. - if (this.namedValues.TryGetValue(node.Name, out value)) - { - this.valueStack.Push(value); - } - else - { - throw new Exception("Unknown variable name"); - } - - return node; - } - - protected override ExprAST VisitBinaryExprAST(BinaryExprAST node) - { - this.Visit(node.Lhs); - this.Visit(node.Rhs); - - LLVMValueRef r = this.valueStack.Pop(); - LLVMValueRef l = this.valueStack.Pop(); - - LLVMValueRef n; - - switch (node.NodeType) - { - case ExprType.AddExpr: - n = LLVM.BuildFAdd(this.builder, l, r, "addtmp"); - break; - case ExprType.SubtractExpr: - n = LLVM.BuildFSub(this.builder, l, r, "subtmp"); - break; - case ExprType.MultiplyExpr: - n = LLVM.BuildFMul(this.builder, l, r, "multmp"); - break; - case ExprType.LessThanExpr: - // Convert bool 0/1 to double 0.0 or 1.0 - n = LLVM.BuildUIToFP(this.builder, LLVM.BuildFCmp(this.builder, LLVMRealPredicate.LLVMRealULT, l, r, "cmptmp"), LLVM.DoubleType(), "booltmp"); - break; - default: - throw new Exception("invalid binary operator"); - } - - this.valueStack.Push(n); - return node; - } - - protected override ExprAST VisitCallExprAST(CallExprAST node) - { - var calleeF = LLVM.GetNamedFunction(this.module, node.Callee); - if (calleeF.Pointer == IntPtr.Zero) - { - throw new Exception("Unknown function referenced"); - } - - if (LLVM.CountParams(calleeF) != node.Arguments.Count) - { - throw new Exception("Incorrect # arguments passed"); - } - - var argumentCount = (uint)node.Arguments.Count; - var argsV = new LLVMValueRef[Math.Max(argumentCount, 1)]; - for (int i = 0; i < argumentCount; ++i) - { - this.Visit(node.Arguments[i]); - argsV[i] = this.valueStack.Pop(); - } - - valueStack.Push(LLVM.BuildCall(this.builder, calleeF, argsV, "calltmp")); - - return node; - } - - protected override ExprAST VisitPrototypeAST(PrototypeAST node) - { - // Make the function type: double(double,double) etc. - var argumentCount = (uint)node.Arguments.Count; - var arguments = new LLVMTypeRef[Math.Max(argumentCount, 1)]; - - var function = LLVM.GetNamedFunction(this.module, node.Name); - - // If F conflicted, there was already something named 'Name'. If it has a - // body, don't allow redefinition or reextern. - if (function.Pointer != IntPtr.Zero) - { - // If F already has a body, reject this. - if (LLVM.CountBasicBlocks(function) != 0) - { - throw new Exception("redefinition of function."); - } - - // If F took a different number of args, reject. - if (LLVM.CountParams(function) != argumentCount) - { - throw new Exception("redefinition of function with different # args"); - } - } - else - { - for (int i = 0; i < argumentCount; ++i) - { - arguments[i] = LLVM.DoubleType(); - } - - function = LLVM.AddFunction(this.module, node.Name, LLVM.FunctionType(LLVM.DoubleType(), arguments, LLVMBoolFalse)); - LLVM.SetLinkage(function, LLVMLinkage.LLVMExternalLinkage); - } - - for (int i = 0; i < argumentCount; ++i) - { - string argumentName = node.Arguments[i]; - - LLVMValueRef param = LLVM.GetParam(function, (uint)i); - LLVM.SetValueName(param, argumentName); - - this.namedValues[argumentName] = param; - } - - this.valueStack.Push(function); - return node; - } - - protected override ExprAST VisitFunctionAST(FunctionAST node) - { - this.namedValues.Clear(); - - this.Visit(node.Proto); - - LLVMValueRef function = this.valueStack.Pop(); - - // Create a new basic block to start insertion into. - LLVM.PositionBuilderAtEnd(this.builder, LLVM.AppendBasicBlock(function, "entry")); - - try - { - this.Visit(node.Body); - } - catch (Exception) - { - LLVM.DeleteFunction(function); - throw; - } - - // Finish off the function. - LLVM.BuildRet(this.builder, this.valueStack.Pop()); - - // Validate the generated code, checking for consistency. - LLVM.VerifyFunction(function, LLVMVerifierFailureAction.LLVMPrintMessageAction); - - this.valueStack.Push(function); - - return node; - } - - protected override ExprAST VisitIfExprAST(IfExpAST node) - { - this.Visit(node.Condition); - var condv = LLVM.BuildFCmp(this.builder, LLVMRealPredicate.LLVMRealONE, this.valueStack.Pop(), LLVM.ConstReal(LLVM.DoubleType(), 0.0), "ifcond"); - - LLVMValueRef func = LLVM.GetBasicBlockParent(LLVM.GetInsertBlock(builder)); - - // Create blocks for the then and else cases. Insert the 'then' block at the - // end of the function. - LLVMBasicBlockRef thenBB = LLVM.AppendBasicBlock(func, "then"); - LLVMBasicBlockRef elseBB = LLVM.AppendBasicBlock(func, "else"); - LLVMBasicBlockRef mergeBB = LLVM.AppendBasicBlock(func, "ifcont"); - - LLVM.BuildCondBr(this.builder, condv, thenBB, elseBB); - - // Emit then value. - LLVM.PositionBuilderAtEnd(this.builder, thenBB); - - this.Visit(node.Then); - var thenV = this.valueStack.Pop(); - - LLVM.BuildBr(this.builder, mergeBB); - - // Codegen of 'Then' can change the current block, update ThenBB for the PHI. - thenBB = LLVM.GetInsertBlock(this.builder); - - // Emit else block. - - LLVM.PositionBuilderAtEnd(this.builder, elseBB); - - this.Visit(node.Else); - var elseV = this.valueStack.Pop(); - - LLVM.BuildBr(this.builder, mergeBB); - - // Codegen of 'Else' can change the current block, update ElseBB for the PHI. - elseBB = LLVM.GetInsertBlock(this.builder); - - // Emit merge block. - LLVM.PositionBuilderAtEnd(this.builder, mergeBB); - var phi = LLVM.BuildPhi(this.builder, LLVM.DoubleType(), "iftmp"); - - LLVM.AddIncoming(phi, new []{thenV}, new []{thenBB}, 1); - LLVM.AddIncoming(phi, new []{elseV}, new []{elseBB}, 1); - - this.valueStack.Push(phi); - - return node; - } - - protected override ExprAST VisitForExprAST(ForExprAST node) - { - // Output this as: - // ... - // start = startexpr - // goto loop - // loop: - // variable = phi [start, loopheader], [nextvariable, loopend] - // ... - // bodyexpr - // ... - // loopend: - // step = stepexpr - // nextvariable = variable + step - // endcond = endexpr - // br endcond, loop, endloop - // outloop: - - // Emit the start code first, without 'variable' in scope. - this.Visit(node.Start); - var startVal = this.valueStack.Pop(); - - // Make the new basic block for the loop header, inserting after current - // block. - var preheaderBB = LLVM.GetInsertBlock(this.builder); - var function = LLVM.GetBasicBlockParent(preheaderBB); - var loopBB = LLVM.AppendBasicBlock(function, "loop"); - - // Insert an explicit fall through from the current block to the LoopBB. - LLVM.BuildBr(this.builder, loopBB); - - // Start insertion in LoopBB. - LLVM.PositionBuilderAtEnd(this.builder, loopBB); - - // Start the PHI node with an entry for Start. - var variable = LLVM.BuildPhi(builder, LLVM.DoubleType(), node.VarName); - LLVM.AddIncoming(variable, new []{startVal}, new []{preheaderBB}, 1); - - // Within the loop, the variable is defined equal to the PHI node. If it - // shadows an existing variable, we have to restore it, so save it now. - LLVMValueRef oldVal; - if (this.namedValues.TryGetValue(node.VarName, out oldVal)) - { - this.namedValues[node.VarName] = variable; - } - else - { - this.namedValues.Add(node.VarName, variable); - } - - // Emit the body of the loop. This, like any other expr, can change the - // current BB. Note that we ignore the value computed by the body, but don't - // allow an error. - Visit(node.Body); - - // Emit the step value. - LLVMValueRef stepVal; - if (node.Step != null) - { - Visit(node.Step); - stepVal = valueStack.Pop(); - } - else - { - // If not specified, use 1.0. - stepVal = LLVM.ConstReal(LLVM.DoubleType(), 1.0); - } - - LLVMValueRef nextVar = LLVM.BuildFAdd(builder, variable, stepVal, "nextvar"); - - // Compute the end condition. - Visit(node.End); - LLVMValueRef endCond = LLVM.BuildFCmp(builder, LLVMRealPredicate.LLVMRealONE, valueStack.Pop(), LLVM.ConstReal(LLVM.DoubleType(), 0.0), "loopcond"); - - // Create the "after loop" block and insert it. - var loopEndBB = LLVM.GetInsertBlock(builder); - var afterBB = LLVM.AppendBasicBlock(function, "afterloop"); - - // Insert the conditional branch into the end of LoopEndBB. - LLVM.BuildCondBr(builder, endCond, loopBB, afterBB); - - // Any new code will be inserted in AfterBB. - LLVM.PositionBuilderAtEnd(builder, afterBB); - - // Add a new entry to the PHI node for the backedge. - LLVM.AddIncoming(variable, new []{nextVar}, new []{loopEndBB}, 1); - - // Restore the unshadowed variable. - if (oldVal.Pointer != IntPtr.Zero) - { - namedValues[node.VarName] = oldVal; - } - else - { - namedValues.Remove(node.VarName); - } - - valueStack.Push(LLVM.ConstReal(LLVM.DoubleType(), 0.0)); - - return node; - } - } -} \ No newline at end of file diff --git a/samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/KaleidoscopeLLVM.csproj b/samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/KaleidoscopeLLVM.csproj deleted file mode 100644 index 3bf1f997..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/KaleidoscopeLLVM.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - net8.0 - - - - - - - - - - diff --git a/samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/Program.cs b/samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/Program.cs deleted file mode 100644 index ea156bfd..00000000 --- a/samples/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/Program.cs +++ /dev/null @@ -1,114 +0,0 @@ -namespace KaleidoscopeLLVM -{ - using System; - using System.Collections.Generic; - using System.Runtime.InteropServices; - using Kaleidoscope; - using LLVMSharp; - - public sealed class Program - { - public delegate double D(); - - private static void Main(string[] args) - { - // Make the module, which holds all the code. - LLVMModuleRef module = LLVM.ModuleCreateWithName("my cool jit"); - LLVMBuilderRef builder = LLVM.CreateBuilder(); - - LLVM.LinkInMCJIT(); - LLVM.InitializeX86TargetInfo(); - LLVM.InitializeX86Target(); - LLVM.InitializeX86TargetMC(); - - if (LLVM.CreateExecutionEngineForModule(out var engine, module, out var errorMessage).Value == 1) - { - Console.WriteLine(errorMessage); - // LLVM.DisposeMessage(errorMessage); - return; - } - - // Create a function pass manager for this engine - LLVMPassManagerRef passManager = LLVM.CreateFunctionPassManagerForModule(module); - - // Set up the optimizer pipeline. Start with registering info about how the - // target lays out data structures. - // LLVM.DisposeTargetData(LLVM.GetExecutionEngineTargetData(engine)); - - // Provide basic AliasAnalysis support for GVN. - LLVM.AddBasicAliasAnalysisPass(passManager); - - // Promote allocas to registers. - LLVM.AddPromoteMemoryToRegisterPass(passManager); - - // Do simple "peephole" optimizations and bit-twiddling optzns. - LLVM.AddInstructionCombiningPass(passManager); - - // Reassociate expressions. - LLVM.AddReassociatePass(passManager); - - // Eliminate Common SubExpressions. - LLVM.AddGVNPass(passManager); - - // Simplify the control flow graph (deleting unreachable blocks, etc). - LLVM.AddCFGSimplificationPass(passManager); - - LLVM.InitializeFunctionPassManager(passManager); - - var codeGenlistener = new CodeGenParserListener(engine, passManager, new CodeGenVisitor(module, builder)); - - // Install standard binary operators. - // 1 is lowest precedence. - var binopPrecedence = new Dictionary - { - ['<'] = 10, - ['+'] = 20, - ['-'] = 20, - ['*'] = 40 - }; - // highest. - - var scanner = new Lexer(Console.In, binopPrecedence); - var parser = new Parser(scanner, codeGenlistener); - - // Prime the first token. - Console.Write("ready> "); - scanner.GetNextToken(); - - // Run the main "interpreter loop" now. - MainLoop(scanner, parser); - - // Print out all of the generated code. - LLVM.DumpModule(module); - - LLVM.DisposeModule(module); - LLVM.DisposePassManager(passManager); - } - - private static void MainLoop(ILexer lexer, IParser parser) - { - // top ::= definition | external | expression | ';' - while (true) - { - Console.Write("ready> "); - switch (lexer.CurrentToken) - { - case (int)Token.EOF: - return; - case ';': - lexer.GetNextToken(); - break; - case (int)Token.DEF: - parser.HandleDefinition(); - break; - case (int)Token.EXTERN: - parser.HandleExtern(); - break; - default: - parser.HandleTopLevelExpression(); - break; - } - } - } - } -}