public AssemblyElement GetProperPushInstruction(DatSymbol symbol, int arrIndex) { BaseExecBlockContext activeBlock = ActiveExecBlock; if (arrIndex > 0) { return(new PushArrayVar(symbol, arrIndex)); } if (IsInsideArgList) { return(PushSymbol(symbol, FuncCallCtx.GetParameterType())); } if (IsInsideReturnStatement && activeBlock != null) { return(PushSymbol(symbol, activeBlock.GetSymbol().ReturnType)); } if (IsInsideAssignment) { return(PushSymbol(symbol, AssignmentType)); } if (IsInsideIfCondition) { return(PushSymbol(symbol, DatSymbolType.Int)); } return(PushSymbol(symbol)); }
public List <AssemblyElement> GetDatSymbolReferenceInstructions(DatSymbolReference reference) { List <AssemblyElement> instructions = new List <AssemblyElement>(); if (reference.HasAttribute()) { bool isInsideExecBlock = ActiveExecBlock != null; bool isSymbolSelf = reference.Object == ActiveExecBlock?.GetSymbol(); // self.attribute, slf.attribute cases bool isSymbolPassedToInstanceParameter = IsInsideArgList && FuncCallCtx.GetParameterType() == DatSymbolType.Instance; bool isSymbolPassedToFuncParameter = IsInsideArgList && FuncCallCtx.GetParameterType() == DatSymbolType.Func; bool isInsideFuncAssignment = IsInsideAssignment && AssignmentType == DatSymbolType.Func; if (isInsideExecBlock && !isSymbolSelf && !isInsideFuncAssignment && !(isSymbolPassedToInstanceParameter || isSymbolPassedToFuncParameter) ) { instructions.Add(new SetInstance(reference.Object)); } instructions.Add(GetProperPushInstruction(reference.Attribute, reference.Index)); } else { instructions.Add(GetProperPushInstruction(reference.Object, reference.Index)); } return(instructions); }
public override void ExitParameterList(DaedalusParser.ParameterListContext context) { var parameterDeclContexts = context.parameterDecl(); foreach (var parameterDeclContext in parameterDeclContexts.Reverse()) { BaseExecBlockContext baseExecBlock = _assemblyBuilder.ExecBlocks.Last(); string execBlockName = baseExecBlock.GetSymbol().Name; string parameterLocalName = parameterDeclContext.nameNode().GetText(); string parameterName = $"{execBlockName}.{parameterLocalName}"; DatSymbol parameterSymbol = _assemblyBuilder.ResolveSymbol(parameterName); var assignInstruction = AssemblyBuilderHelpers.GetAssignInstructionForDatSymbolType(parameterSymbol.Type); if (parameterSymbol.Type is DatSymbolType.Instance) { _assemblyBuilder.AddInstruction(new PushInstance(parameterSymbol)); } else { _assemblyBuilder.AddInstruction(new PushVar(parameterSymbol)); } _assemblyBuilder.AddInstruction(assignInstruction); } }
public override void EnterVarDecl([NotNull] DaedalusParser.VarDeclContext context) { if (context.Parent.Parent is DaedalusParser.DaedalusFileContext || _assemblyBuilder.IsContextInsideExecBlock()) { var typeName = context.typeReference().GetText(); var type = DatSymbolTypeFromString(typeName); if (type == DatSymbolType.Class) { type = DatSymbolType.Instance; } for (int i = 0; i < context.ChildCount; i++) { var varContext = context.GetChild(i); if (varContext is TerminalNodeImpl) { continue; // skips ',' } if (varContext is DaedalusParser.VarValueDeclContext varValueContext) { var name = varValueContext.nameNode().GetText(); if (_assemblyBuilder.IsContextInsideExecBlock()) { // TODO consider making assemblyBuilder.active public and using it here BaseExecBlockContext baseExecBlock = _assemblyBuilder.ExecBlocks.Last(); string execBlockName = baseExecBlock.GetSymbol().Name; name = $"{execBlockName}.{name}"; } var location = GetLocation(context); int parentIndex = DatSymbol.NULL_INDEX; string parameterTypeName = context.typeReference().GetText(); DatSymbolType?parameterType = DatSymbolTypeFromString(parameterTypeName); if (parameterType is DatSymbolType.Class) { var parentSymbol = _assemblyBuilder.ResolveSymbol(parameterTypeName); parentIndex = parentSymbol.Index; } var symbol = SymbolBuilder.BuildVariable(name, type, location, parentIndex); // TODO : Validate params _assemblyBuilder.AddSymbol(symbol); } if (varContext is DaedalusParser.VarArrayDeclContext varArrayContext) { var name = varArrayContext.nameNode().GetText(); var location = GetLocation(context); var size = EvaluatorHelper.EvaluteArraySize(varArrayContext.arraySize(), _assemblyBuilder); var symbol = SymbolBuilder.BuildArrOfVariables(name, type, (uint)size, location); // TODO : Validate params _assemblyBuilder.AddSymbol(symbol); } } } }
public override void EnterParameterDecl(DaedalusParser.ParameterDeclContext context) { BaseExecBlockContext baseExecBlock = _assemblyBuilder.ExecBlocks.Last(); string execBlockName = baseExecBlock.GetSymbol().Name; string parameterLocalName = context.nameNode().GetText(); string parameterName = $"{execBlockName}.{parameterLocalName}"; int parentIndex = DatSymbol.NULL_INDEX; string parameterTypeName = context.typeReference().GetText(); DatSymbolType?parameterType = DatSymbolTypeFromString(parameterTypeName); if (parameterType is DatSymbolType.Class) { parameterType = DatSymbolType.Instance; var parentSymbol = _assemblyBuilder.ResolveSymbol(parameterTypeName); parentIndex = parentSymbol.Index; } DatSymbol symbol; var location = GetLocation(context); var arraySizeContext = context.arraySize(); if (arraySizeContext != null) { if (!uint.TryParse(arraySizeContext.GetText(), out var arrIndex)) { var constSymbol = _assemblyBuilder.ResolveSymbol(arraySizeContext.GetText()); if (constSymbol.Flags != DatSymbolFlag.Const || constSymbol.Type != DatSymbolType.Int) { throw new Exception($"Expected integer constant: {arraySizeContext.GetText()}"); } arrIndex = (uint)(int)constSymbol.Content[0]; } symbol = SymbolBuilder.BuildArrOfVariables(parameterName, parameterType.Value, arrIndex, location); } else { if (_assemblyBuilder.IsCurrentlyParsingExternals) { symbol = SymbolBuilder.BuildExternalParameter(parameterName, parameterType.Value, location, parentIndex); } else { symbol = SymbolBuilder.BuildParameter(parameterName, parameterType.Value, location, parentIndex); } } _assemblyBuilder.AddSymbol(symbol); }
public DatSymbol GetReferenceAtomSymbol(DaedalusParser.ReferenceContext referenceContext) { var referenceAtoms = referenceContext.referenceAtom(); var referenceAtom = referenceAtoms[0]; string symbolNameLower = referenceAtom.Identifier().GetText().ToLower(); bool isSymbolNameSelfKeyword = symbolNameLower == "slf" || symbolNameLower == "self"; if (ActiveExecBlock != null && isSymbolNameSelfKeyword) { bool isDottedReference = IsDottedReference(referenceContext); DatSymbolType activeSymbolType = ActiveExecBlock.GetSymbol().Type; if ( activeSymbolType == DatSymbolType.Instance || (activeSymbolType == DatSymbolType.Prototype && isDottedReference) ) { return(ActiveExecBlock.GetSymbol()); } } return(ResolveSymbol(referenceAtom.Identifier().GetText())); }
public override void EnterConstDef([NotNull] DaedalusParser.ConstDefContext context) { _assemblyBuilder.IsInsideConstDef = true; var typeName = context.typeReference().GetText(); var type = DatSymbolTypeFromString(typeName); if (type == DatSymbolType.Func) { type = DatSymbolType.Int; } for (int i = 0; i < context.ChildCount; i++) { var constContext = context.GetChild(i); if (constContext is TerminalNodeImpl) { continue; // skips ',' } if (constContext is DaedalusParser.ConstValueDefContext constValueContext) { var name = constValueContext.nameNode().GetText(); if (_assemblyBuilder.IsContextInsideExecBlock()) { BaseExecBlockContext baseExecBlock = _assemblyBuilder.ExecBlocks.Last(); string execBlockName = baseExecBlock.GetSymbol().Name; name = $"{execBlockName}.{name}"; } var location = GetLocation(context); var assignmentExpression = constValueContext.constValueAssignment().expressionBlock().expression(); var value = EvaluatorHelper.EvaluateConst(assignmentExpression, _assemblyBuilder, type); var symbol = SymbolBuilder.BuildConst(name, type, value, location); // TODO : Validate params _assemblyBuilder.AddSymbol(symbol); continue; } if (constContext is DaedalusParser.ConstArrayDefContext constArrayContext) { _assemblyBuilder.ErrorContext.Context = constArrayContext; var name = constArrayContext.nameNode().GetText(); var location = GetLocation(context); int declaredSize = 0; bool compareDeclaredSizeAndElementsCount = true; try { _assemblyBuilder.ErrorContext.Context = constArrayContext.arraySize(); declaredSize = EvaluatorHelper.EvaluteArraySize(constArrayContext.arraySize(), _assemblyBuilder); } catch (System.ArgumentNullException) { compareDeclaredSizeAndElementsCount = false; } if (declaredSize == 0 && compareDeclaredSizeAndElementsCount) { compareDeclaredSizeAndElementsCount = false; _assemblyBuilder.Errors.Add( new InvalidArraySizeError( _assemblyBuilder.ErrorContext, name, declaredSize ) ); } var elements = constArrayContext.constArrayAssignment().expressionBlock() .Select(expr => EvaluatorHelper.EvaluateConst(expr.expression(), _assemblyBuilder, type)) .ToArray(); if (compareDeclaredSizeAndElementsCount && declaredSize != elements.Length) { _assemblyBuilder.Errors.Add( new InconsistentArraySizeError( _assemblyBuilder.ErrorContext, name, declaredSize, elements.Length ) ); } var symbol = SymbolBuilder.BuildArrOfConst(name, type, elements, location); // TODO : Validate params _assemblyBuilder.AddSymbol(symbol); } } }