diff --git a/source/XSharp/Compiler.cs b/source/XSharp/Compiler.cs index 4c04b230..fc43994a 100644 --- a/source/XSharp/Compiler.cs +++ b/source/XSharp/Compiler.cs @@ -15,7 +15,7 @@ public class Compiler public bool EmitUserComments = true; public bool EmitSourceCode = true; - private string _currentNamespace = null; + private string _currentNamespace; /// /// Gets the current namespace. @@ -31,6 +31,11 @@ public string CurrentNamespace set => _currentNamespace = value; } + /// + /// Gets the current function. + /// + public string CurrentFunction { get; set; } + public Compiler(TextWriter aOut) { Out = aOut; @@ -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 } diff --git a/source/XSharp/Emitters/AllEmitters.cs b/source/XSharp/Emitters/AllEmitters.cs index 177c2ff9..87ba2a28 100644 --- a/source/XSharp/Emitters/AllEmitters.cs +++ b/source/XSharp/Emitters/AllEmitters.cs @@ -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}:"); } // } diff --git a/source/XSharp/Emitters/Namespace.cs b/source/XSharp/Emitters/Namespace.cs index ca4dbc1b..357b41d5 100644 --- a/source/XSharp/Emitters/Namespace.cs +++ b/source/XSharp/Emitters/Namespace.cs @@ -17,10 +17,10 @@ public Namespace(Compiler aCompiler, x86.Assemblers.Assembler aAsm) : base(aComp /// /// Definition of a namespace. Does not generate any code. /// - [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; } } } diff --git a/source/XSharp/Tokens/Keywords.cs b/source/XSharp/Tokens/Keywords.cs index 65766c84..ac87a5e1 100644 --- a/source/XSharp/Tokens/Keywords.cs +++ b/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") { } }