コード例 #1
0
        private void AnalzyeStandardInputStatement(StandardInputStatementNode standardInput)
        {
            Symbol identifier = standardInput.Parent.LookupSymbol(standardInput.Identifier.IdentifierName);

            if (identifier.Type is VariableTypeSignature)
            {
                identifier.Initialized = true;
            }
            else
            {
                throw new CobaltTypeError($"Standard input called with an identifier that is not a variable (`{identifier.Identifier}`).", standardInput.Identifier.SourceLine);
            }
        }
コード例 #2
0
        private StatementNode ParseStandardInputStatement(List <Token> tokens)
        {
            if (tokens.Count != 2 ||
                tokens.ElementAt(0).Type != TokenType.StandardInput ||
                tokens.ElementAt(1).Type != TokenType.Identifier)
            {
                throw new CobaltSyntaxError($"Ivalid standard input statement.", tokens.First().SourceLine, tokens.First().PositionOnLine);
            }

            // Parse identifier
            IdentifierNode identifier = ParseIdentifier(tokens.Last());

            // Create and return input statement node
            StandardInputStatementNode statement = new StandardInputStatementNode(tokens.First().SourceLine);

            statement.Identifier = identifier;
            return(statement);
        }
コード例 #3
0
        private void GenerateStandardInputStatementCode(StringBuilder builder, StandardInputStatementNode standardInputStatement)
        {
            string identifier = standardInputStatement.Identifier.IdentifierName;

            // Lookup variable symbol
            Symbol variable = null;

            try
            {
                variable = standardInputStatement.LookupSymbol(identifier);
            }
            catch (UndeclaredIdentifierError)
            {
                throw new CompilerException($"`{MethodBase.GetCurrentMethod().Name}` failed looking up symbol `{identifier}`.");
            }

            // Get input with hint about type
            if (variable.Type is VariableTypeSignature variableType)
            {
                switch (variableType.CobaltType)
                {
                case CobaltType.Boolean:
                    builder.Append($"{identifier}=await $cobalt_stdin('Input <{variableType.CobaltType}>: ') == 'true';");
                    break;

                case CobaltType.Float:
                    builder.Append($"{identifier}=parseFloat(await $cobalt_stdin('Input <{variableType.CobaltType}>: '));");
                    break;

                case CobaltType.Integer:
                    builder.Append($"{identifier}=parseInt(await $cobalt_stdin('Input <{variableType.CobaltType}>: '));");
                    break;

                default:
                    throw new CompilerException($"Unknown Cobalt type `{variableType.CobaltType}` encountered in {MethodBase.GetCurrentMethod().Name}.");
                }
            }
            else
            {
                throw new CompilerException($"Symbol looked up in `{MethodBase.GetCurrentMethod().Name}` for identifier `{identifier}` doesn't have a variable type signature.");
            }
        }