From 20f022ccaa944f05e174e6aa68022499d02bc870 Mon Sep 17 00:00:00 2001 From: Jacob Misirian Date: Tue, 15 Aug 2017 20:02:31 -0600 Subject: [PATCH] Fixed major bugs with use --- src/Hassium/Compiler/Emit/HassiumCompiler.cs | 34 ++++++------ src/Hassium/Compiler/Parser/Parser.cs | 55 +++++++++++++------- src/Hassium/Program.cs | 3 ++ src/Hassium/Runtime/HassiumClass.cs | 3 +- src/Hassium/Runtime/HassiumMethod.cs | 25 +++++---- src/Hassium/Runtime/VirtualMachine.cs | 6 +-- 6 files changed, 77 insertions(+), 49 deletions(-) diff --git a/src/Hassium/Compiler/Emit/HassiumCompiler.cs b/src/Hassium/Compiler/Emit/HassiumCompiler.cs index a25e332..2085c5c 100644 --- a/src/Hassium/Compiler/Emit/HassiumCompiler.cs +++ b/src/Hassium/Compiler/Emit/HassiumCompiler.cs @@ -37,7 +37,7 @@ public HassiumModule Compile(AstNode ast) module = new HassiumModule(); classStack.Push(new HassiumClass("__global__")); - methodStack.Push(new HassiumMethod("__init__") { Parent = classStack.Peek() }); + methodStack.Push(new HassiumMethod(module, "__init__") { Parent = classStack.Peek() }); ast.Visit(this); @@ -122,7 +122,7 @@ public void Accept(ClassDeclarationNode node) foreach (var inherit in node.Inherits) { - methodStack.Push(new HassiumMethod() { Parent = classStack.Peek() }); + methodStack.Push(new HassiumMethod(module) { Parent = classStack.Peek() }); inherit.Visit(this); emit(inherit.SourceLocation, InstructionType.Return); clazz.Inherits.Add(methodStack.Pop()); @@ -178,7 +178,7 @@ public void Accept(DoWhileNode node) } public void Accept(EnforcedAssignmentNode node) { - methodStack.Push(new HassiumMethod()); + methodStack.Push(new HassiumMethod(module)); methodStack.Peek().Parent = classStack.Peek(); node.Type.Visit(this); emit(node.Type.SourceLocation, InstructionType.Return); @@ -296,7 +296,7 @@ public void Accept(FunctionCallNode node) } public void Accept(FunctionDeclarationNode node) { - var method = new HassiumMethod(node.Name); + var method = new HassiumMethod(module, node.Name); method.IsPrivate = node.IsPrivate; methodStack.Push(method); method.SourceLocation = node.SourceLocation; @@ -311,7 +311,7 @@ public void Accept(FunctionDeclarationNode node) { if (param.FunctionParameterType == FunctionParameterType.Enforced) { - methodStack.Push(new HassiumMethod()); + methodStack.Push(new HassiumMethod(module)); methodStack.Peek().Parent = classStack.Peek(); param.Type.Visit(this); emit(param.Type.SourceLocation, InstructionType.Return); @@ -327,7 +327,7 @@ public void Accept(FunctionDeclarationNode node) if (node.EnforcedReturnType != null) { - methodStack.Push(new HassiumMethod()); + methodStack.Push(new HassiumMethod(module)); node.EnforcedReturnType.Visit(this); emit(node.EnforcedReturnType.SourceLocation, InstructionType.Return); method.ReturnType = methodStack.Pop(); @@ -389,7 +389,7 @@ public void Accept(IterableAccessNode node) } public void Accept(LambdaNode node) { - var lambda = new HassiumMethod(); + var lambda = new HassiumMethod(module); methodStack.Push(lambda); lambda.Parent = classStack.Peek(); @@ -477,7 +477,7 @@ public void Accept(TernaryOperationNode node) } public void Accept(ThreadNode node) { - var method = new HassiumMethod(); + var method = new HassiumMethod(module); methodStack.Push(method); method.Parent = classStack.Peek(); @@ -500,7 +500,7 @@ public void Accept(TraitNode node) foreach (var pair in node.Attributes) { - methodStack.Push(new HassiumMethod()); + methodStack.Push(new HassiumMethod(module)); pair.Value.Visit(this); emit(pair.Value.SourceLocation, InstructionType.Return); var type = methodStack.Pop(); @@ -514,7 +514,7 @@ public void Accept(TryCatchNode node) { var endLabel = nextLabel(); var temp = methodStack.Peek(); - methodStack.Push(new HassiumMethod("catch")); + methodStack.Push(new HassiumMethod(module, "catch")); methodStack.Peek().Parent = classStack.Peek(); table.EnterScope(); methodStack.Peek().Parameters.Add(new FunctionParameter(FunctionParameterType.Normal, "value"), table.HandleSymbol("value")); @@ -602,14 +602,16 @@ public void Accept(UseFromNode node) if (mod.Attributes.ContainsKey("__global__")) { var globalClass = mod.Attributes["__global__"]; + foreach (var attrib in globalClass.Attributes) + if (attrib.Key == "__init__") + foreach (var instruction in (attrib.Value as HassiumMethod).Instructions) + methodStack.Peek().Instructions.Add(instruction); + if (node.Class == "*") { foreach (var attrib in globalClass.Attributes) { - if (attrib.Key == "__init__") - foreach (var instruction in (attrib.Value as HassiumMethod).Instructions) - methodStack.Peek().Instructions.Add(instruction); - else if (!classStack.Peek().Attributes.ContainsKey(attrib.Key)) + if (!classStack.Peek().Attributes.ContainsKey(attrib.Key)) { var value = attrib.Value.Clone() as HassiumObject; value.Parent = classStack.Peek(); @@ -726,7 +728,7 @@ private HassiumObject resolveModuleByPath(SourceLocation location, string path) return CompileModuleFromFilePath(filePath); } - private string locateFile(string path, string extension) + private string locateFile(string path, string extension, bool pass = false) { if (File.Exists(path)) return path; @@ -748,6 +750,8 @@ private string locateFile(string path, string extension) return homeFilePath; if (File.Exists(homeFilePath + extension)) return homeFilePath + extension; + if (!pass) + return locateFile(Path.Combine(Program.MasterPath, path), extension, true); return string.Empty; } } diff --git a/src/Hassium/Compiler/Parser/Parser.cs b/src/Hassium/Compiler/Parser/Parser.cs index 311aa3d..c349608 100644 --- a/src/Hassium/Compiler/Parser/Parser.cs +++ b/src/Hassium/Compiler/Parser/Parser.cs @@ -213,23 +213,37 @@ private UseFromNode parseFrom() var location = this.location; expectToken(TokenType.Identifier, "from"); - StringBuilder module = new StringBuilder(); - if (matchToken(TokenType.String)) - module.Append(expectToken(TokenType.String).Value); + var first = new StringBuilder(); + + while (matchToken(TokenType.Dot) || matchToken(TokenType.Operation, "/")) + first.Append(tokens[position++].Value); + do + { + first.Append(expectToken(TokenType.Identifier).Value); + if (matchToken(TokenType.Dot) || matchToken(TokenType.Operation, "/")) + first.Append("/"); + } + while (acceptToken(TokenType.Dot) || acceptToken(TokenType.Operation, "/")); + + expectToken(TokenType.Identifier, "use"); + + var second = new StringBuilder(); + if (matchToken(TokenType.Operation, "*")) + second.Append(expectToken(TokenType.Operation, "*").Value); else { + while (matchToken(TokenType.Dot) || matchToken(TokenType.Operation, "/")) + second.Append(tokens[position++].Value); do { + second.Append(expectToken(TokenType.Identifier).Value); if (matchToken(TokenType.Dot) || matchToken(TokenType.Operation, "/")) - module.Append(tokens[position].Value); + second.Append("/"); } while (acceptToken(TokenType.Dot) || acceptToken(TokenType.Operation, "/")); } - expectToken(TokenType.Identifier, "use"); - string clazz = tokens[position++].Value; - - return new UseFromNode(location, clazz, module.ToString()); + return new UseFromNode(location, second.ToString(), first.ToString()); } private FunctionDeclarationNode parseFunctionDeclaration() @@ -411,19 +425,20 @@ private AstNode parseUse() expectToken(TokenType.Identifier, "use"); var first = new StringBuilder(); - if (acceptToken(TokenType.Operation, "*")) + if (matchToken(TokenType.Operation, "*")) first.Append(expectToken(TokenType.Operation, "*").Value); - - while (matchToken(TokenType.Dot) || matchToken(TokenType.Operation, "/")) - first.Append(tokens[position++].Value); - do + else { - first.Append(expectToken(TokenType.Identifier).Value); - if (matchToken(TokenType.Dot) || matchToken(TokenType.Operation, "/")) + while (matchToken(TokenType.Dot) || matchToken(TokenType.Operation, "/")) first.Append(tokens[position++].Value); + do + { + first.Append(expectToken(TokenType.Identifier).Value); + if (matchToken(TokenType.Dot) || matchToken(TokenType.Operation, "/")) + first.Append("/"); + } + while (acceptToken(TokenType.Dot) || acceptToken(TokenType.Operation, "/")); } - while (matchToken(TokenType.Dot) || matchToken(TokenType.Operation, "/")); - if (!acceptToken(TokenType.Identifier, "from")) return new UseNode(location, first.ToString()); @@ -434,10 +449,10 @@ private AstNode parseUse() { second.Append(expectToken(TokenType.Identifier).Value); if (matchToken(TokenType.Dot) || matchToken(TokenType.Operation, "/")) - second.Append(tokens[position++].Value); + second.Append("/"); } - while (matchToken(TokenType.Dot) || matchToken(TokenType.Operation, "/")); - + while (acceptToken(TokenType.Dot) || acceptToken(TokenType.Operation, "/")); + return new UseFromNode(location, first.ToString(), second.ToString()); } diff --git a/src/Hassium/Program.cs b/src/Hassium/Program.cs index ca8c749..3dfa4f7 100644 --- a/src/Hassium/Program.cs +++ b/src/Hassium/Program.cs @@ -9,10 +9,13 @@ namespace Hassium { public class Program { + public static string MasterPath = string.Empty; + static void Main(string[] args) { try { + MasterPath = System.IO.Directory.GetCurrentDirectory(); VirtualMachine vm = new VirtualMachine(); var module = HassiumCompiler.CompileModuleFromFilePath(args[0]); HassiumList hargs = new HassiumList(new HassiumObject[0]); diff --git a/src/Hassium/Runtime/HassiumClass.cs b/src/Hassium/Runtime/HassiumClass.cs index ccf52c7..8574896 100644 --- a/src/Hassium/Runtime/HassiumClass.cs +++ b/src/Hassium/Runtime/HassiumClass.cs @@ -22,7 +22,8 @@ public HassiumClass(string name) public new void AddAttribute(string name, HassiumObject obj) { obj.Parent = this; - Attributes.Add(name, obj); + if (!Attributes.ContainsKey(name)) + Attributes.Add(name, obj); } public override HassiumObject Invoke(VirtualMachine vm, SourceLocation location, params HassiumObject[] args) diff --git a/src/Hassium/Runtime/HassiumMethod.cs b/src/Hassium/Runtime/HassiumMethod.cs index 56da68a..11b7b20 100644 --- a/src/Hassium/Runtime/HassiumMethod.cs +++ b/src/Hassium/Runtime/HassiumMethod.cs @@ -14,22 +14,25 @@ public class HassiumMethod : HassiumObject { public static new HassiumTypeDefinition TypeDefinition = new HassiumTypeDefinition("func"); - public string Name { get; set; } - public bool IsConstructor { get { return Name == "new"; } } - - public SourceLocation SourceLocation { get; set; } - public string SourceRepresentation { get; set; } - public Stack BreakLabels { get; private set; } public Stack ContinueLabels { get; private set; } - public Dictionary Labels { get; private set; } public List Instructions { get; private set; } + public bool IsConstructor { get { return Name == "new"; } } + + public Dictionary Labels { get; private set; } + + public HassiumModule Module { get; private set; } + public string Name { get; set; } + public Dictionary Parameters { get; private set; } public HassiumMethod ReturnType { get; set; } - public HassiumMethod() + public SourceLocation SourceLocation { get; set; } + public string SourceRepresentation { get; set; } + + public HassiumMethod(HassiumModule module) { BreakLabels = new Stack(); ContinueLabels = new Stack(); @@ -37,20 +40,22 @@ public HassiumMethod() Labels = new Dictionary(); Parameters = new Dictionary(); + Module = module; Name = string.Empty; SourceRepresentation = string.Empty; AddAttribute(INVOKE, Invoke); AddType(TypeDefinition); } - public HassiumMethod(string name) + public HassiumMethod(HassiumModule module, string name) { BreakLabels = new Stack(); ContinueLabels = new Stack(); Instructions = new List(); Labels = new Dictionary(); Parameters = new Dictionary(); - + + Module = module; Name = name; SourceRepresentation = string.Empty; diff --git a/src/Hassium/Runtime/VirtualMachine.cs b/src/Hassium/Runtime/VirtualMachine.cs index 74adf26..00771b7 100644 --- a/src/Hassium/Runtime/VirtualMachine.cs +++ b/src/Hassium/Runtime/VirtualMachine.cs @@ -170,7 +170,9 @@ public HassiumObject ExecuteMethod(HassiumMethod method) break; case InstructionType.LoadGlobal: attrib = CurrentModule.ConstantPool[arg]; - if (Globals.ContainsKey(attrib)) + if (method.Module.Attributes["__global__"].Attributes.ContainsKey(attrib)) + Stack.Push(method.Module.Attributes["__global__"].Attributes[attrib]); + else if (Globals.ContainsKey(attrib)) Stack.Push(Globals[attrib]); else if (method.Parent != null) { @@ -411,8 +413,6 @@ private void interpretUnaryOperation(HassiumObject target, int op) public void RaiseException(HassiumObject message) { - Console.WriteLine("Raising exception {0}", message.ToString(this, CurrentSourceLocation).String); - Console.WriteLine("\n\n\n"); if (Handlers.Count == 0) { var callStack = UnwindCallStack();