Skip to content

Commit

Permalink
Merge pull request #12 from CosmosOS/feature/ImplementFunction
Browse files Browse the repository at this point in the history
Implement function emitter
  • Loading branch information
charlesbetros committed Dec 22, 2018
2 parents 83ede95 + 44c0a91 commit 5156a2f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
15 changes: 12 additions & 3 deletions source/XSharp/Compiler.cs
Expand Up @@ -15,7 +15,7 @@ public class Compiler
public bool EmitUserComments = true;
public bool EmitSourceCode = true;

private string _currentNamespace = null;
private string _currentNamespace;

/// <summary>
/// Gets the current namespace.
Expand All @@ -31,6 +31,11 @@ public string CurrentNamespace
set => _currentNamespace = value;
}

/// <summary>
/// Gets the current function.
/// </summary>
public string CurrentFunction { get; set; }

public Compiler(TextWriter aOut)
{
Out = aOut;
Expand Down Expand Up @@ -98,9 +103,13 @@ public void Emit(TextReader aIn)

#region Helper methods for namspaces

public string GetPrefixForConst => $"{CurrentNamespace}_Const_";
public string GetPrefixForConst => string.IsNullOrWhiteSpace(CurrentFunction)
? $"{CurrentNamespace}_Const_"
: $"{CurrentNamespace}_{CurrentFunction}_Const_";

public string GetPrefixForVar => $"{CurrentNamespace}_Var_";
public string GetPrefixForVar => string.IsNullOrWhiteSpace(CurrentFunction)
? $"{CurrentNamespace}_Var_"
: $"{CurrentNamespace}_{CurrentFunction}_Var_";

#endregion Helper methods for namspaces
}
Expand Down
6 changes: 4 additions & 2 deletions source/XSharp/Emitters/AllEmitters.cs
Expand Up @@ -168,9 +168,11 @@ protected void InterruptDefinitionStart(string interruptKeyword, string function
}

// function fName123 {
[Emitter(typeof(Function), typeof(Identifier), typeof(OpOpenBrace))]
protected void FunctionDefinitionStart(string funcKeyword, string functionName, string opOpenBraces)
[Emitter(typeof(FunctionKeyword), typeof(Identifier), typeof(OpOpenBrace))]
protected void FunctionDefinitionStart(string aFunctionKeyword, string aFunctionName, string opOpenBraces)
{
Compiler.CurrentFunction = aFunctionName;
Compiler.WriteLine($"{Compiler.CurrentNamespace}_{aFunctionName}:");
}

// }
Expand Down
6 changes: 3 additions & 3 deletions source/XSharp/Emitters/Namespace.cs
Expand Up @@ -17,10 +17,10 @@ public Namespace(Compiler aCompiler, x86.Assemblers.Assembler aAsm) : base(aComp
/// <summary>
/// Definition of a namespace. Does not generate any code.
/// </summary>
[Emitter(typeof(NamespaceKeyword), typeof(AlphaNum))] // namespace name
protected void NamespaceDefinition(string aNamespaceKeyword, string aText)
[Emitter(typeof(NamespaceKeyword), typeof(Identifier))] // namespace name
protected void NamespaceDefinition(string aNamespaceKeyword, string aNamespaceName)
{
Compiler.CurrentNamespace = aText;
Compiler.CurrentNamespace = aNamespaceName;
}
}
}
6 changes: 3 additions & 3 deletions source/XSharp/Tokens/Keywords.cs
@@ -1,15 +1,15 @@
namespace XSharp.Tokens
{
public class NamespaceKeyword : Spruce.Tokens.AlphaNumList
public class NamespaceKeyword: Spruce.Tokens.AlphaNumList
{
public NamespaceKeyword() : base("Namespace")
{
}
}

public class Function : Spruce.Tokens.AlphaNumList
public class FunctionKeyword : Spruce.Tokens.AlphaNumList
{
public Function() : base("Function")
public FunctionKeyword() : base("Function")
{
}
}
Expand Down

0 comments on commit 5156a2f

Please sign in to comment.