Example #1
0
        private bool parameter()
        {
            int line     = _line;
            int position = _position;

            string type = oneOf(_dataTypes);

            if (type == null)
            {
                return(false);
            }

            if (!identifier())
            {
                Log.Error("Expected identifier", _filename, _line, _position);
            }

            Node     lvalue               = _stack.Pop();
            DataType dataType             = _dataTypeMapping[type];
            IdentifierDefinitionNode node = new IdentifierDefinitionNode(dataType, lvalue, null, _filename, line, position);

            _stack.Push(node);

            return(true);
        }
Example #2
0
        private bool identifierDefinition()
        {
            int line     = _line;
            int position = _position;

            if (!parameter())
            {
                return(false);
            }

            IdentifierDefinitionNode identifier = (IdentifierDefinitionNode)_stack.Pop();
            Node rvalue = null;

            // default value
            if (accept("="))
            {
                expression();
                rvalue           = _stack.Pop();
                identifier.Value = rvalue;
            }

            // function definition
            else if (accept("("))
            {
                // create new scope here so that parameters don't leak out
                SymbolTable.EnterScope();
                List <Node> parameters = parameterList();
                block();
                SymbolTable.ExitScope();

                Node functionBody = _stack.Pop();

                CallableType type = new CallableType(identifier.Type, parameters, functionBody);
                SymbolTable.CurrentScope.Callables[identifier.Symbol.Identifier] = type;

                FunctionDefinitionNode node = new FunctionDefinitionNode(type, identifier.Symbol, _filename, line, position);
                _stack.Push(node);

                return(true);
            }

            _stack.Push(identifier);
            return(true);
        }