Strings
This commit is contained in:
@@ -17,11 +17,6 @@ public class Generator
|
||||
|
||||
public string Generate()
|
||||
{
|
||||
foreach (var externFuncDefinition in _definitions.OfType<ExternFuncDefinitionNode>())
|
||||
{
|
||||
GenerateExternFuncDefinition(externFuncDefinition);
|
||||
}
|
||||
|
||||
foreach (var funcDefinition in _definitions.OfType<LocalFuncDefinitionNode>())
|
||||
{
|
||||
GenerateFuncDefinition(funcDefinition);
|
||||
@@ -30,12 +25,40 @@ public class Generator
|
||||
return _builder.ToString();
|
||||
}
|
||||
|
||||
private void GenerateExternFuncDefinition(ExternFuncDefinitionNode externFuncDefinition)
|
||||
private string QbeTypeName(NubType type)
|
||||
{
|
||||
if (type.Equals(NubType.Int64))
|
||||
{
|
||||
return "l";
|
||||
}
|
||||
|
||||
if (type.Equals(NubType.Int32))
|
||||
{
|
||||
return "w";
|
||||
}
|
||||
|
||||
if (type.Equals(NubType.String))
|
||||
{
|
||||
return "l";
|
||||
}
|
||||
|
||||
throw new Exception($"Invalid qbe type {type}");
|
||||
}
|
||||
|
||||
private void GenerateFuncDefinition(LocalFuncDefinitionNode node)
|
||||
{
|
||||
var parameters = node.Parameters.Select(p => $"{QbeTypeName(p.Type)} %{p.Name}");
|
||||
_builder.Append("function ");
|
||||
if (node.ReturnType.HasValue)
|
||||
{
|
||||
_builder.Append($"{QbeTypeName(node.ReturnType.Value)} ");
|
||||
}
|
||||
|
||||
_builder.Append(node.Name);
|
||||
_builder.AppendLine($"({string.Join(", ", parameters)}) {{");
|
||||
_builder.AppendLine("@start");
|
||||
GenerateBlock(node.Body, _symbolTable.ResolveLocalFunc(node.Name, node.Parameters.Select(x => x.Type).ToList()));
|
||||
_builder.AppendLine("}");
|
||||
}
|
||||
|
||||
private void GenerateBlock(BlockNode block, LocalFuncDef func)
|
||||
@@ -65,9 +88,6 @@ public class Generator
|
||||
case ReturnNode @return:
|
||||
GenerateReturn(@return, func);
|
||||
break;
|
||||
case SyscallStatementNode syscallStatement:
|
||||
GenerateSyscall(syscallStatement.Syscall, func);
|
||||
break;
|
||||
case VariableAssignmentNode variableAssignment:
|
||||
GenerateVariableAssignment(variableAssignment, func);
|
||||
break;
|
||||
@@ -132,9 +152,6 @@ public class Generator
|
||||
case StructMemberAccessorNode structMemberAccessor:
|
||||
GenerateStructMemberAccessor(structMemberAccessor, func);
|
||||
break;
|
||||
case SyscallExpressionNode syscallExpression:
|
||||
GenerateSyscall(syscallExpression.Syscall, func);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(expression));
|
||||
}
|
||||
@@ -163,8 +180,4 @@ public class Generator
|
||||
private void GenerateFuncCall(FuncCall funcCall, LocalFuncDef func)
|
||||
{
|
||||
}
|
||||
|
||||
private void GenerateSyscall(Syscall syscall, LocalFuncDef func)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ public class SymbolTable
|
||||
var externFuncDefs = new List<ExternFuncDef>();
|
||||
var localFuncDefs = new List<LocalFuncDef>();
|
||||
|
||||
var strings = new List<string>();
|
||||
|
||||
foreach (var node in program)
|
||||
{
|
||||
switch (node)
|
||||
@@ -29,7 +31,7 @@ public class SymbolTable
|
||||
var parameters = localFuncDefinitionNode.Parameters.Select(parameter => new Variable(parameter.Name, parameter.Type)).ToList();
|
||||
var localVariables = new List<Variable>();
|
||||
|
||||
FindVariables(localFuncDefinitionNode.Body);
|
||||
Search(localFuncDefinitionNode.Body);
|
||||
|
||||
localFuncDefs.Add(new LocalFuncDef
|
||||
{
|
||||
@@ -40,27 +42,97 @@ public class SymbolTable
|
||||
});
|
||||
break;
|
||||
|
||||
void FindVariables(BlockNode blockNode)
|
||||
void Search(BlockNode blockNode)
|
||||
{
|
||||
foreach (var statement in blockNode.Statements)
|
||||
{
|
||||
switch (statement)
|
||||
{
|
||||
case FuncCallStatementNode funcCallStatementNode:
|
||||
{
|
||||
foreach (var parameter in funcCallStatementNode.FuncCall.Parameters)
|
||||
{
|
||||
FindStrings(parameter);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case IfNode ifNode:
|
||||
{
|
||||
FindVariables(ifNode.Body);
|
||||
SearchIf(ifNode);
|
||||
break;
|
||||
}
|
||||
case ReturnNode returnNode:
|
||||
{
|
||||
if (returnNode.Value.HasValue)
|
||||
{
|
||||
FindStrings(returnNode.Value.Value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case WhileNode whileNode:
|
||||
{
|
||||
FindVariables(whileNode.Body);
|
||||
FindStrings(whileNode.Condition);
|
||||
Search(whileNode.Body);
|
||||
break;
|
||||
}
|
||||
case VariableAssignmentNode variableAssignmentNode:
|
||||
{
|
||||
FindStrings(variableAssignmentNode.Value);
|
||||
localVariables.Add(new Variable(variableAssignmentNode.Name, variableAssignmentNode.Value.Type));
|
||||
break;
|
||||
}
|
||||
case VariableReassignmentNode variableReassignmentNode:
|
||||
{
|
||||
FindStrings(variableReassignmentNode.Value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SearchIf(IfNode ifNode)
|
||||
{
|
||||
FindStrings(ifNode.Condition);
|
||||
Search(ifNode.Body);
|
||||
if (ifNode.Else.HasValue)
|
||||
{
|
||||
ifNode.Else.Value.Match(SearchIf, Search);
|
||||
}
|
||||
}
|
||||
|
||||
void FindStrings(ExpressionNode expressionNode)
|
||||
{
|
||||
switch (expressionNode)
|
||||
{
|
||||
case BinaryExpressionNode binaryExpressionNode:
|
||||
{
|
||||
FindStrings(binaryExpressionNode.Left);
|
||||
FindStrings(binaryExpressionNode.Right);
|
||||
break;
|
||||
}
|
||||
case FuncCallExpressionNode funcCallExpressionNode:
|
||||
{
|
||||
foreach (var parameter in funcCallExpressionNode.FuncCall.Parameters)
|
||||
{
|
||||
FindStrings(parameter);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LiteralNode literalNode:
|
||||
{
|
||||
if (literalNode.LiteralType.Equals(NubType.String))
|
||||
{
|
||||
strings.Add(literalNode.Literal);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case StructInitializerNode structInitializerNode:
|
||||
{
|
||||
foreach (var initializer in structInitializerNode.Initializers)
|
||||
{
|
||||
FindStrings(initializer.Value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,18 +148,31 @@ public class SymbolTable
|
||||
}
|
||||
}
|
||||
|
||||
return new SymbolTable(externFuncDefs, localFuncDefs);
|
||||
return new SymbolTable(strings, externFuncDefs, localFuncDefs);
|
||||
}
|
||||
|
||||
private readonly List<string> _strings;
|
||||
private readonly List<ExternFuncDef> _externFuncDefs;
|
||||
private readonly List<LocalFuncDef> _localFuncDefs;
|
||||
|
||||
private SymbolTable(List<ExternFuncDef> externFuncDefs, List<LocalFuncDef> localFuncDefs)
|
||||
private SymbolTable(List<string> strings, List<ExternFuncDef> externFuncDefs, List<LocalFuncDef> localFuncDefs)
|
||||
{
|
||||
_strings = strings;
|
||||
_externFuncDefs = externFuncDefs;
|
||||
_localFuncDefs = localFuncDefs;
|
||||
}
|
||||
|
||||
public int ResolveString(string value)
|
||||
{
|
||||
var index = _strings.IndexOf(value);
|
||||
if (index == -1)
|
||||
{
|
||||
throw new Exception("String not found: " + value);
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
public FuncDef ResolveFunc(string name, List<NubType> parameters)
|
||||
{
|
||||
var matching = _externFuncDefs.Concat<FuncDef>(_localFuncDefs).Where(funcDef => funcDef.SignatureMatches(name, parameters)).ToArray();
|
||||
|
||||
Reference in New Issue
Block a user