Skip to content

Commit

Permalink
Fixed major bugs with use
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobMisirian committed Aug 16, 2017
1 parent 9824647 commit 20f022c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 49 deletions.
34 changes: 19 additions & 15 deletions src/Hassium/Compiler/Emit/HassiumCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -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();
Expand All @@ -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"));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down
55 changes: 35 additions & 20 deletions src/Hassium/Compiler/Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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());

Expand All @@ -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());
}

Expand Down
3 changes: 3 additions & 0 deletions src/Hassium/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
3 changes: 2 additions & 1 deletion src/Hassium/Runtime/HassiumClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 15 additions & 10 deletions src/Hassium/Runtime/HassiumMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,48 @@ 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<int> BreakLabels { get; private set; }
public Stack<int> ContinueLabels { get; private set; }
public Dictionary<int, int> Labels { get; private set; }

public List<HassiumInstruction> Instructions { get; private set; }

public bool IsConstructor { get { return Name == "new"; } }

public Dictionary<int, int> Labels { get; private set; }

public HassiumModule Module { get; private set; }
public string Name { get; set; }

public Dictionary<FunctionParameter, int> 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<int>();
ContinueLabels = new Stack<int>();
Instructions = new List<HassiumInstruction>();
Labels = new Dictionary<int, int>();
Parameters = new Dictionary<FunctionParameter, int>();

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<int>();
ContinueLabels = new Stack<int>();
Instructions = new List<HassiumInstruction>();
Labels = new Dictionary<int, int>();
Parameters = new Dictionary<FunctionParameter, int>();


Module = module;
Name = name;
SourceRepresentation = string.Empty;

Expand Down
6 changes: 3 additions & 3 deletions src/Hassium/Runtime/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 20f022c

Please sign in to comment.