public static ParameterParseResult GetParameters(ParseInfo parseInfo, Scope methodScope, DeltinScriptParser.SetParametersContext context) { if (context == null) { return(new ParameterParseResult(new CodeParameter[0], new Var[0])); } var parameters = new CodeParameter[context.define().Length]; var vars = new Var[parameters.Length]; for (int i = 0; i < context.define().Length; i++) { var newVar = Var.CreateVarFromContext(VariableDefineType.Parameter, parseInfo, context.define(i)); newVar.Finalize(methodScope); vars[i] = newVar; ExpressionOrWorkshopValue initialValue = null; if (newVar.InitialValue != null) { initialValue = new ExpressionOrWorkshopValue(newVar.InitialValue); } parameters[i] = new CodeParameter(context.define(i).name.Text, newVar.CodeType, initialValue); } return(new ParameterParseResult(parameters, vars)); }
public static IStatement GetStatement(ParseInfo parseInfo, Scope scope, DeltinScriptParser.StatementContext statementContext) { switch (statementContext) { case DeltinScriptParser.S_defineContext define: { var newVar = Var.CreateVarFromContext(VariableDefineType.Scoped, parseInfo, define.define()); newVar.Finalize(scope); return(new DefineAction(newVar)); } case DeltinScriptParser.S_methodContext method: return(new CallMethodAction(parseInfo, scope, method.method(), false, scope)); case DeltinScriptParser.S_varsetContext varset: return(new SetVariableAction(parseInfo, scope, varset.varset())); case DeltinScriptParser.S_exprContext s_expr: { var expr = GetExpression(parseInfo, scope, s_expr.expr(), true, false); if (expr is ExpressionTree == false || ((ExpressionTree)expr)?.Result is IStatement == false) { if (expr != null) { parseInfo.Script.Diagnostics.Error("Expressions can't be used as statements.", DocRange.GetRange(statementContext)); } return(null); } else { return((ExpressionTree)expr); } } case DeltinScriptParser.S_ifContext s_if: return(new IfAction(parseInfo, scope, s_if.@if())); case DeltinScriptParser.S_whileContext s_while: return(new WhileAction(parseInfo, scope, s_while.@while())); case DeltinScriptParser.S_forContext s_for: return(new ForAction(parseInfo, scope, s_for.@for())); case DeltinScriptParser.S_foreachContext s_foreach: return(new ForeachAction(parseInfo, scope, s_foreach.@foreach())); case DeltinScriptParser.S_returnContext s_return: return(new ReturnAction(parseInfo, scope, s_return.@return())); case DeltinScriptParser.S_deleteContext s_delete: return(new DeleteAction(parseInfo, scope, s_delete.delete())); default: return(null); } }
public ForAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.ForContext forContext) { Scope varScope = scope.Child(); if (forContext.define() != null) { DefinedVariable = Var.CreateVarFromContext(VariableDefineType.Scoped, parseInfo, forContext.define()); DefinedVariable.Finalize(varScope); } else if (forContext.initialVarset != null) { InitialVarSet = new SetVariableAction(parseInfo, varScope, forContext.initialVarset); } if (forContext.expr() != null) { Condition = DeltinScript.GetExpression(parseInfo, varScope, forContext.expr()); } if (forContext.endingVarset != null) { SetVariableAction = new SetVariableAction(parseInfo, varScope, forContext.endingVarset); } // Get the block. if (forContext.block() != null) { Block = new BlockAction(parseInfo, varScope, forContext.block()); // Get the path info. Path = new PathInfo(Block, DocRange.GetRange(forContext.FOR()), false); } else { parseInfo.Script.Diagnostics.Error("Expected a block.", DocRange.GetRange(forContext.RIGHT_PAREN())); } }
public DefinedType(ParseInfo parseInfo, Scope scope, DeltinScriptParser.Type_defineContext typeContext, List <IApplyBlock> applyMethods) : base(typeContext.name.Text) { this.translateInfo = parseInfo.TranslateInfo; if (translateInfo.IsCodeType(Name)) { parseInfo.Script.Diagnostics.Error($"A type with the name '{Name}' already exists.", DocRange.GetRange(typeContext.name)); } DefinedAt = new LanguageServer.Location(parseInfo.Script.Uri, DocRange.GetRange(typeContext.name)); translateInfo.AddSymbolLink(this, DefinedAt); if (typeContext.CLASS() != null) { TypeKind = TypeKind.Class; TypeKindString = "class"; } else if (typeContext.STRUCT() != null) { TypeKind = TypeKind.Struct; TypeKindString = "struct"; } else { throw new NotImplementedException(); } staticScope = translateInfo.GlobalScope.Child(TypeKindString + " " + Name); objectScope = staticScope.Child(TypeKindString + " " + Name); // Get the variables defined in the type. foreach (var definedVariable in typeContext.define()) { Var newVar = Var.CreateVarFromContext(VariableDefineType.InClass, parseInfo, definedVariable); newVar.Finalize(UseScope(newVar.Static)); if (!newVar.Static) { objectVariables.Add(newVar); } } // Todo: Static methods/macros. foreach (var definedMethod in typeContext.define_method()) { var newMethod = new DefinedMethod(parseInfo, UseScope(false), definedMethod); applyMethods.Add(newMethod); } foreach (var macroContext in typeContext.define_macro()) { DeltinScript.GetMacro(parseInfo, UseScope(false), macroContext, applyMethods); } // Get the constructors. if (typeContext.constructor().Length > 0) { Constructors = new Constructor[typeContext.constructor().Length]; for (int i = 0; i < Constructors.Length; i++) { Constructors[i] = new DefinedConstructor(parseInfo, this, typeContext.constructor(i)); applyMethods.Add((DefinedConstructor)Constructors[i]); } } else { // If there are no constructors, create a default constructor. Constructors = new Constructor[] { new Constructor(this, new Location(parseInfo.Script.Uri, DocRange.GetRange(typeContext.name)), AccessLevel.Public) }; } }
void Translate() { List <IApplyBlock> applyMethods = new List <IApplyBlock>(); // Get the reserved variables and IDs foreach (ScriptFile script in ScriptFiles) { if (script.Context.reserved_global()?.reserved_list() != null) { foreach (var name in script.Context.reserved_global().reserved_list().PART()) { VarCollection.Reserve(name.GetText(), true); } foreach (var id in script.Context.reserved_global().reserved_list().NUMBER()) { VarCollection.Reserve(int.Parse(id.GetText()), true, null, null); } } if (script.Context.reserved_player()?.reserved_list() != null) { foreach (var name in script.Context.reserved_player().reserved_list().PART()) { VarCollection.Reserve(name.GetText(), false); } foreach (var id in script.Context.reserved_player().reserved_list().NUMBER()) { VarCollection.Reserve(int.Parse(id.GetText()), false, null, null); } } } // Get the enums foreach (ScriptFile script in ScriptFiles) { foreach (var enumContext in script.Context.enum_define()) { var newEnum = new DefinedEnum(new ParseInfo(script, this), enumContext); types.Add(newEnum); definedTypes.Add(newEnum); } } // Get the types foreach (ScriptFile script in ScriptFiles) { foreach (var typeContext in script.Context.type_define()) { var newType = new DefinedType(new ParseInfo(script, this), GlobalScope, typeContext, applyMethods); types.Add(newType); definedTypes.Add(newType); } } // Get the methods and macros foreach (ScriptFile script in ScriptFiles) { // Get the methods. foreach (var methodContext in script.Context.define_method()) { var newMethod = new DefinedMethod(new ParseInfo(script, this), RulesetScope, methodContext); applyMethods.Add(newMethod); //RulesetScope.AddMethod(newMethod, script.Diagnostics, DocRange.GetRange(methodContext.name)); } // Get the macros. foreach (var macroContext in script.Context.define_macro()) { GetMacro(new ParseInfo(script, this), RulesetScope, macroContext, applyMethods); } } // Get the defined variables. foreach (ScriptFile script in ScriptFiles) { foreach (var varContext in script.Context.define()) { var newVar = Var.CreateVarFromContext(VariableDefineType.RuleLevel, new ParseInfo(script, this), varContext); newVar.Finalize(RulesetScope); rulesetVariables.Add(newVar); // Add the variable to the player variables scope if it is a player variable. if (newVar.VariableType == VariableType.Player) { PlayerVariableScope.AddVariable(newVar, null, null); } } } foreach (var apply in applyMethods) { apply.SetupBlock(); } foreach (var apply in applyMethods) { apply.CallInfo.CheckRecursion(); } // Get the rules foreach (ScriptFile script in ScriptFiles) { foreach (var ruleContext in script.Context.ow_rule()) { rules.Add(new RuleAction(new ParseInfo(script, this), RulesetScope, ruleContext)); } } }