示例#1
0
        protected override void Translate()
        {
            var expressionText = GenUtils.Translate_StringLiteral(RootParseNode);

            if (
                expressionText.First() == '$' &&
                expressionText.Last() == '$' &&
                expressionText.Count(c => c == '$') == 2
                )
            {
                //If the expression is on the form '$ anything $' convert it into a normal multivector expression
                //based on (anything) alone; not a symbolic expression
                expressionText = expressionText.Substring(1, expressionText.Length - 2).Trim();

                _generatedExpression = translate_LanguageExpression(expressionText);

                return;
            }

            MathematicaScalar scalar;

            OperandsByName operands;

            translate_Dependency_List(expressionText, out scalar, out operands);

            _generatedExpression = BasicExpressionGenerator.Generate_SymbolicExpression(scalar, operands);
        }
示例#2
0
        private void translate_Dependency_List(string expressionText, out MathematicaScalar scalar, out OperandsByName operands)
        {
            var finalScalarText = new StringBuilder(expressionText);

            operands = OperandsByName.Create();

            var varIdx = 1;

            var allMatches = GenUtils.ExtractDistinctInternalExpressions(expressionText);

            foreach (var rgexMatch in allMatches)
            {
                var rhsExprText = rgexMatch.Value.Substring(1, rgexMatch.Value.Length - 2);

                var rhsExpr =
                    BasicExpressionGenerator.Generate_PolyadicOperand(
                        GMacRootAst.ScalarType,
                        translate_LanguageExpression(rhsExprText)
                        );

                var lhsVarName = "var" + (varIdx++).ToString("0000");

                finalScalarText = finalScalarText.Replace(rgexMatch.Value, lhsVarName);

                operands.AddOperand(lhsVarName, rhsExpr);
            }

            scalar = MathematicaScalar.Create(Cas, finalScalarText.ToString());
        }
示例#3
0
        //public override void ResetOnAcquire()
        //{
        //    base.ResetOnAcquire();

        //    _vSpaceDim = 0;
        //    _baseFrame = null;
        //    _generatedFrame = null;
        //}


        ///Read the list of basis vectors names for the current frame
        private string[] translate_Frame_Vectors(ParseTreeNode node)
        {
            node.Assert(GMacParseNodeNames.FrameVectors);

            var basisVectorsNames = new List <string>();

            foreach (var subnode in node.ChildNodes)
            {
                var basisVectorName = GenUtils.Translate_Identifier(subnode);

                if (basisVectorsNames.Contains(basisVectorName))
                {
                    CompilationLog.RaiseGeneratorError <int>("Basis vector name already defined", subnode);
                }

                basisVectorsNames.Add(basisVectorName);
            }

            if (basisVectorsNames.Count > FrameUtils.MaxVSpaceDimension)
            {
                CompilationLog.RaiseGeneratorError <int>("Cannot handle spaces with dimension larger than " + FrameUtils.MaxVSpaceDimension, node);
            }

            return(basisVectorsNames.ToArray());
        }
        private void translate_Outerproduct_List_SingleIdentifier(ParseTreeNode node)
        {
            var identName = GenUtils.Translate_Identifier(node);

            GMacFrameSubspace subspace;

            if (_frame.LookupSubspace(identName, out subspace))
            {
                AddBasisBladeIDs(subspace);
            }

            else
            {
                GMacFrameBasisVector basisVector;
                if (_frame.LookupBasisVector(identName, out basisVector))
                {
                    AddBasisBladeId(basisVector.BasisVectorId);
                }

                else
                {
                    translate_PredefinedBasisBladeIDs(identName, node);
                }
            }
        }
示例#5
0
        ///Set the signature of the current frame to a signature vector (diagonal IPM)
        private GaFrame translate_Frame_Signature_Orthogonal(ParseTreeNode node)
        {
            var bvSigVector = MathematicaVector.Create(Cas, GenUtils.Translate_StringLiteral(node.ChildNodes[0]));

            if (bvSigVector.Size != _vSpaceDim)
            {
                CompilationLog.RaiseGeneratorError <int>("Expecting a vector with " + _vSpaceDim + " items", node.ChildNodes[0]);
            }

            return(GaFrame.CreateOrthogonal(bvSigVector));
        }
示例#6
0
        ///Set the signature of the current frame to a signature vector of +1's and -1's (diagonal IPM)
        private GaFrame translate_Frame_Signature_Orthonormal(ParseTreeNode node)
        {
            var bvSigString = GenUtils.Translate_StringLiteral(node.ChildNodes[0]).Trim();

            if (bvSigString.Count(c => c == '+' || c == '-') != _vSpaceDim || bvSigString.Length != _vSpaceDim)
            {
                CompilationLog.RaiseGeneratorError <int>("Expecting a vector of " + _vSpaceDim + @" (+\-) items", node.ChildNodes[0]);
            }

            return(GaFrame.CreateOrthonormal(bvSigString));
        }
示例#7
0
        ///Set the signature of the current frame to be defined by IPM
        private GaFrame translate_Frame_Signature_IPM(ParseTreeNode node)
        {
            //Read the IPM symbolic matrix
            var ipmMatrix = MathematicaMatrix.Create(Cas, GenUtils.Translate_StringLiteral(node.ChildNodes[0]));

            if (ipmMatrix.IsSymmetric() == false || ipmMatrix.Rows != _vSpaceDim)
            {
                CompilationLog.RaiseGeneratorError <int>("Expecting a square symmetric matrix with " + _vSpaceDim + " rows", node.ChildNodes[0]);
            }

            return(GaFrame.CreateFromIpm(ipmMatrix));
        }
示例#8
0
        /// <summary>
        /// For the given parent scope this method extracts an identifier from the given node and
        /// returns it if it isn't already used for a child symbol of the scope.
        /// Else it raises a generator exception
        /// </summary>
        /// <param name="parentScope"></param>
        /// <param name="node"></param>
        /// <returns></returns>
        protected string TranslateChildSymbolName(LanguageScope parentScope, ParseTreeNode node)
        {
            //Read the name of the new child symbol
            var childSymbolName = GenUtils.Translate_Identifier(node);

            //Make sure the child symbol name is not used inside the parent scope
            if (parentScope.SymbolExists(childSymbolName))
            {
                CompilationLog.RaiseGeneratorError <int>("Child symbol name already used", node);
            }

            return(childSymbolName);
        }
示例#9
0
        private CommandDeclareVariable translate_Identifier_Declaration(ParseTreeNode node)
        {
            //Read the name of the member
            var identifierName = GenUtils.Translate_Identifier(node.ChildNodes[0]);

            if (Context.ActiveParentScope.SymbolExists(identifierName))
            {
                return(CompilationLog.RaiseGeneratorError <CommandDeclareVariable>("Identifier name already used", node.ChildNodes[0]));
            }

            var identifierType = GMacValueAccessGenerator.Translate_Direct_LanguageType(Context, node.ChildNodes[1]);

            return(translate_Declare(identifierName, identifierType));
        }
        //public override void ResetOnAcquire()
        //{
        //    base.ResetOnAcquire();

        //    _generatedExpression = null;
        //}


        //public GMacExpressionBasicGenerator BasicExpressionGenerator { get; private set; }


        //private GMacExpressionCompositeGenerator(GMacSymbolTranslatorContext context)
        //    : base(context)
        //{
        //    //BasicExpressionGenerator = new GMacExpressionBasicGenerator(context);
        //}

        //private GMacExpressionCompositeGenerator(GMacExpressionBasicGenerator basic_expr_gen)
        //    : base(basic_expr_gen.Context)
        //{
        //    BasicExpressionGenerator = basic_expr_gen;
        //}


        private SymbolLocalVariable translate_Identifier_Declaration(ParseTreeNode node)
        {
            //Read the name of the member
            var identifierName = GenUtils.Translate_Identifier(node.ChildNodes[0]);

            if (Context.ActiveParentScope.SymbolExists(identifierName))
            {
                return(CompilationLog.RaiseGeneratorError <SymbolLocalVariable>("Identifier name already used", node.ChildNodes[0]));
            }

            var identifierType = GMacValueAccessGenerator.Translate_Direct_LanguageType(Context, node.ChildNodes[1]);

            //Create the member in the symbol table
            return
                (Context
                 .ActiveParentCompositeExpression
                 .DefineLocalVariable(identifierName, identifierType)
                 .LocalVariable);
        }
示例#11
0
        //protected void SetContext(GMacSymbolTranslatorContext context)
        //{
        //    this.SetContext((SymbolTranslatorContext)context);
        //}

        /// <summary>
        /// Take a qualified indetifier node and separate it into a parent symbol and a child symbol name
        /// </summary>
        /// <param name="node"></param>
        /// <param name="parentSymbol"></param>
        /// <param name="childSymbolName"></param>
        protected bool Translate_ParentSymbolAndChildSymbolName(ParseTreeNode node, out SymbolWithScope parentSymbol, out string childSymbolName)
        {
            var qualList = GenUtils.Translate_Qualified_Identifier(node);

            childSymbolName = qualList.LastItem;

            if (qualList.ActiveLength == 1)
            {
                parentSymbol = Context.ActiveParentSymbol;
            }

            else
            {
                qualList.DecreaseActiveEndOffset();

                parentSymbol = GMacValueAccessGenerator.Translate_Direct(Context, node, qualList) as SymbolWithScope;
            }

            return(ReferenceEquals(parentSymbol, null) == false);
        }
示例#12
0
        private static bool TryTranslate_LValue(GMacSymbolTranslatorContext context, ParseTreeNode node, out LanguageValueAccess varAccess)
        {
            var qualList = GenUtils.Translate_Qualified_Identifier(node);

            LanguageSymbol symbol;

            var flag =
                context
                .OpenedDistinctScopes()
                .LookupSymbol(qualList.FirstItem, out symbol);

            if (flag == false || (symbol is SymbolLValue) == false)
            {
                varAccess = null;
                return(false);
            }

            varAccess = LanguageValueAccess.Create(symbol);

            return(true);
        }
示例#13
0
        //public override void ResetOnAcquire()
        //{
        //    base.ResetOnAcquire();

        //    _generatedMacroTemplate = null;
        //}


        private void translate_MacroTemplate()
        {
            try
            {
                Context.MarkCheckPointState();

                var nodeMacro = RootParseNode.ChildNodes[0];

                //Read the name of the new macro template
                var qualList = GenUtils.Translate_Qualified_Identifier(nodeMacro.ChildNodes[0]);

                if (qualList.ActiveLength > 1)
                {
                    CompilationLog.RaiseGeneratorError <int>("Template name cannot be a qualified name", RootParseNode.ChildNodes[0]);
                }

                var templateName = qualList.FirstItem;

                if (Context.ParentNamespace.CanDefineChildSymbol(templateName) == false)
                {
                    CompilationLog.RaiseGeneratorError <int>("Symbol name already used", RootParseNode.ChildNodes[0]);
                }

                _generatedMacroTemplate =
                    Context.ParentNamespace.DefineMacroTemplate(templateName, nodeMacro);

                _generatedMacroTemplate.CodeLocation = Context.GetCodeLocation(RootParseNode);

                Context.UnmarkCheckPointState();
            }
            catch (CompilerException)
            {
                Context.RestoreToCheckPointState();
            }
            catch (Exception e)
            {
                Context.RestoreToCheckPointState();
                throw new Exception("Unhandled Exception", e);
            }
        }
        private void translate_BasisBladesSet_List_Item_GASpan(ParseTreeNode node)
        {
            var nodeIdentifierList = node.ChildNodes[0];

            //Create the set of unique spanning vectors for the GA subspace
            var basisVectorsList = new List <int>(nodeIdentifierList.ChildNodes.Count);

            foreach (var nodeIdentifier in nodeIdentifierList.ChildNodes)
            {
                GMacFrameBasisVector basisVector;
                var basisVectorName = GenUtils.Translate_Identifier(nodeIdentifier);

                if (_frame.LookupBasisVector(basisVectorName, out basisVector) == false)
                {
                    CompilationLog.RaiseGeneratorError <int>("Basis vector not recognized", nodeIdentifier);
                }

                //Only add unique basis vectors to the spanning set
                if (basisVectorsList.Exists(x => x == basisVector.BasisVectorId) == false)
                {
                    basisVectorsList.Add(basisVector.BasisVectorId);
                }
            }

            //Compute the dimension of the GA spanned by the basis vectors
            var subspaceDimension = 1 << basisVectorsList.Count;

            //Scalars are always part of the GA subspace based on any given set of basis vectors
            AddBasisBladeId(0);

            //Add the remaining basis blades to the GA subspace
            for (var idIndex = 1; idIndex <= subspaceDimension - 1; idIndex++)
            {
                var id = FrameUtils.ComposeGaSubspaceBasisBladeId(basisVectorsList, idIndex);

                AddBasisBladeId(id);
            }
        }
示例#15
0
        ///Set the signature of the current frame to a base frame with a change of basis matrix
        private GaFrame translate_Frame_Signature_CBM(ParseTreeNode node)
        {
            var baseFrame =
                (GMacFrame)GMacValueAccessGenerator.Translate_Direct(Context, node.ChildNodes[0], RoleNames.Frame);

            if (baseFrame.VSpaceDimension != _vSpaceDim)
            {
                CompilationLog.RaiseGeneratorError <int>("Base frame must be of dimension " + _vSpaceDim, node.ChildNodes[0]);
            }

            var cbmMatrix = MathematicaMatrix.Create(Cas, GenUtils.Translate_StringLiteral(node.ChildNodes[1]));

            if (cbmMatrix.IsInvertable() == false || cbmMatrix.Rows != _vSpaceDim)
            {
                CompilationLog.RaiseGeneratorError <int>("Expecting a square invertable matrix with " + _vSpaceDim + " rows", node.ChildNodes[1]);
            }

            _baseFrame = baseFrame;

            var derivedFrameSystem = GaFrame.CreateDerivedCbmFrameSystem(baseFrame.AssociatedSymbolicFrame, cbmMatrix);

            return(derivedFrameSystem.DerivedFrame);
        }
        private void translate_Outerproduct_List(ParseTreeNode node)
        {
            if (node.ChildNodes.Count == 1)
            {
                translate_Outerproduct_List_SingleIdentifier(node.ChildNodes[0]);
            }
            else if (node.ChildNodes.Count > 1)
            {
                var basisVectorsList = new List <GMacFrameBasisVector>(node.ChildNodes.Count);
                var basisBladeId     = 0;

                foreach (var nodeIdentifier in node.ChildNodes)
                {
                    GMacFrameBasisVector basisVector;
                    var basisVectorName = GenUtils.Translate_Identifier(nodeIdentifier);

                    if (_frame.LookupBasisVector(basisVectorName, out basisVector) == false)
                    {
                        CompilationLog.RaiseGeneratorError <int>("Basis vector not recognized", nodeIdentifier);
                    }

                    if (basisVectorsList.Exists(x => x.BasisVectorId == basisVector.BasisVectorId))
                    {
                        CompilationLog.RaiseGeneratorError <int>("Basis blades set not recognized", node);
                    }

                    basisVectorsList.Add(basisVector);
                    basisBladeId = basisBladeId | basisVector.BasisVectorId;
                }

                AddBasisBladeId(basisBladeId);
            }
            else
            {
                CompilationLog.RaiseGeneratorError <int>("Basis blades set not recognized", node);
            }
        }
示例#17
0
        //public override void ResetOnAcquire()
        //{
        //    base.ResetOnAcquire();

        //    _generatedNamespace = null;
        //}


        private void translate_Namespace()
        {
            try
            {
                Context.MarkCheckPointState();

                //Read the namespace name: for example main.conformal.cga5d
                var qualList = GenUtils.Translate_Qualified_Identifier(RootParseNode.ChildNodes[0]);

                //Find the root namespace inside the root global scope of the GMacDSL (search for a namespace called 'main')
                GMacNamespace nameSpace;

                if (GMacRootAst.LookupRootNamespace(qualList.FirstItem, out nameSpace) == false)
                {
                    if (GMacRootAst.RootScope.SymbolExists(qualList.FirstItem))
                    {
                        CompilationLog.RaiseGeneratorError <int>("Namespace name already used", RootParseNode.ChildNodes[0]);
                    }

                    nameSpace = GMacRootAst.DefineRootNamespace(qualList.FirstItem);
                }

                //Starting from the created\found root namespace, repeat the previous operation for each child namespace in qual_list
                for (var i = 1; i < qualList.ActiveLength; i++)
                {
                    GMacNamespace childNamespace;

                    if (nameSpace.LookupNamespace(qualList[i], out childNamespace))
                    {
                        nameSpace = childNamespace;
                    }

                    else
                    {
                        if (nameSpace.CanDefineChildSymbol(qualList[i]) == false)
                        {
                            CompilationLog.RaiseGeneratorError <int>("Symbol with same name already exists", RootParseNode.ChildNodes[0]);
                        }

                        nameSpace = nameSpace.DefineNamespace(qualList[i]);
                    }
                }

                _generatedNamespace = nameSpace;

                _generatedNamespace.AddCodeLocation(Context.GetCodeLocation(RootParseNode));

                Context.UnmarkCheckPointState();

                Context.CompilationLog.ReportNormal("Translated Namespace: " + _generatedNamespace.SymbolAccessName, ProgressEventArgsResult.Success);
            }
            catch (CompilerException)
            {
                Context.RestoreToCheckPointState();
                Context.CompilationLog.ReportNormal("Translate Namespace Failed", ProgressEventArgsResult.Failure);
            }
            catch (Exception e)
            {
                Context.RestoreToCheckPointState();
                Context.CompilationLog.ReportError("Translate Namespace Failed With Error", e);
            }
        }
示例#18
0
        private LanguageValueAccess translate_Qualified_Identifier(ParseTreeNode node)
        {
            //If _QualList is not already filled, translate the current parse node into a list of identifiers
            if (_qualList == null)
            {
                _qualList = GenUtils.Translate_Qualified_Identifier(node);
            }

            //Lookup the first item of the translated list of identifiers within the current context
            LanguageSymbol symbol;

            var flag =
                _followScopeChain
                //? Context.LookupSymbolInOpenedDistinctScopes(_qualList.FirstItem, out symbol)
                ? Context.OpenedDistinctScopes().LookupSymbol(_qualList.FirstItem, out symbol)
                : Context.ActiveParentScope.LookupSymbol(_qualList.FirstItem, out symbol);

            if (flag == false)
            {
                return(CompilationLog.RaiseGeneratorError <LanguageValueAccess>("Symbol name not recognized", RootParseNode));
            }

            //Ignore the first item from the list
            _qualList.IncreaseActiveStartOffset(1);

            if (_isLValue)
            {
                //This is an l-value
                switch (symbol.SymbolRoleName)
                {
                case RoleNames.StructureDataMember:
                case RoleNames.MacroParameter:
                case RoleNames.LocalVariable:
                    return(translate_StartAt_DataSymbol((SymbolDataStore)symbol));

                default:
                    return(CompilationLog.RaiseGeneratorError <LanguageValueAccess>("LValue symbol name not recognized", RootParseNode));
                }
            }

            //This is an r-value
            switch (symbol.SymbolRoleName)
            {
            case RoleNames.MacroParameter:
            case RoleNames.LocalVariable:
            case RoleNames.Constant:
                return(translate_StartAt_DataSymbol((SymbolDataStore)symbol));

            case RoleNames.FrameBasisVector:
            case RoleNames.BuiltinType:
            case RoleNames.FrameMultivector:
            case RoleNames.Structure:
            case RoleNames.Macro:
            case RoleNames.MacroTemplate:
            case RoleNames.Transform:
            case RoleNames.FrameSubspace:
                //case RoleNames.Binding:
                return(translate_StartAt_DirectSymbol(symbol));

            case RoleNames.Frame:
                return(translate_StartAt_FrameDefinition((GMacFrame)symbol));

            case RoleNames.Namespace:
                return(translate_StartAt_NamespaceDefinition((GMacNamespace)symbol));

            default:
                return(CompilationLog.RaiseGeneratorError <LanguageValueAccess>("RValue symbol name not recognized", RootParseNode));
            }
        }