ASPL لباتASPL 0.7.0

Search documentation

Search starts with current English documentation · ASPL 0.7.0

Enter a complete name such as read_line, or words about a topic.

    العربية
    Source files

    Open one UTF-8 .aspl or .لبات file, up to 1 MiB, in a new draft. It stays in this browser and does not run automatically.

    Numeric parsing

    ASPL 0.7.0 · Current release

    Parse strict integer text, inspect the matching result branch and retry invalid input.

    On this page

    What you will learn

    Parse strict integer text, inspect the correct result branch, and retry after invalid input. Explain why 12x fails but the next reply 41 prints 42.

    Before you begin

    Read text input, bindings, conditions and loops. This independent example includes its entry and a complete result-handling template. Open a new Home draft and choose Run explicitly; earlier drafts remain available.

    A result and a retry loop

    parse_i64 / حلل_ص64 takes a string and returns result<i64> / نتيجة<ص64>. A result holds either an integer value or error information. The angle brackets name the success payload type. let parsed binds a new result for each iteration. read_line / اقرأ_سطر still returns text; it does not turn that text into a number.

    Browser and native · ASPL 0.7.0 · English source

    Execution: Browser and native · ASPL 0.7.0

    Prerequisites:
    • Read the example explanation in its lesson before running.

    Input lines: ["12x","41"]

    fn start_main() -> i64 {
        var pending: bool = true;
        while pending {
            print("Number: ");
            let parsed: result<i64> = parse_i64(read_line());
            if (parsed::ok) {
                print(format_i64(parsed::value + 1) + "\n");
                pending = false;
            } else {
                print(parsed::error::code + "\n");
            }
        }
        return 0;
    }
    
    Download lesson-numeric-parsing.aspl

    Expected outcomes
    {
      "browser": {
        "check": "success",
        "class": "success",
        "output": "Number: invalid-number\nNumber: 42\n",
        "processStatus": 0
      },
      "native": {
        "check": 0,
        "status": 0,
        "output": "Number: invalid-number\nNumber: 42\n"
      }
    }

    var pending: bool = true / متغير معلق: منطقي = صواب starts a mutable Boolean flag. while / طالما repeats while that flag is true. Each iteration prints Number: and reads one line. Postfix :: accesses a result field. parsed::ok / محلل::نجاح is the Boolean success test used by if / إذا.

    Only the success branch reads parsed::value / محلل::قيمة, adds 1, formats the integer with format_i64 / نسق_ص64, and prints it with an explicit \n. It then sets pending = false / معلق = خطأ, so the next loop test ends the loop. Only the else / وإلا failure branch reads parsed::error::code / محلل::الخطأ::الرمز. It prints that stable code and a newline, leaving pending true so another reply can be read.

    Never read value on failure or error on success. In the browser, either wrong payload access traps with wrong-result-branch and ends execution. The wrong-branch example reports the same trap on native ASPL 0.7.0 and exits with status 5, without a normal entry status. This is not a handled parsing failure. The template avoids wrong access by testing ok first. A parsing failure is a recoverable result: it produces no runtime diagnostic and permits the retry. A read at EOF still traps with input-exhausted; Stop still cancels. Neither becomes a parsing failure.

    The reserved start_main() -> i64 / ابدأ_الرئيسية() -> ص64 entry returns 0 after the loop. That normal process status is separate from the printed answer and error code.

    Predict two replies

    Before Run, predict the output for 12x, then 41. After the first reply, output is Number: invalid-number\nNumber: and execution waits again. The letter makes the whole first string invalid; the parser does not accept a numeric prefix. The second reply supplies the integer 41, so output becomes exactly Number: invalid-number\nNumber: 42\n, with status 0. Only the second iteration reads the numeric payload. There is no third read.

    Which numeric text is accepted?

    Use an optional ASCII + or - followed by one or more ASCII decimal digits 0 through 9. Leading zeros are allowed. The value must fit i64, from -9223372036854775808 through 9223372036854775807, inclusive.

    Submitted text Parsing result
    41, +41, 041, -1 Success, values 41, 41, 41, -1
    Empty line invalid-number
    41, 41 , or a line containing only spaces invalid-number
    12x, 1.5, 0x29, +, - invalid-number
    Arabic-Indic ١٢ or Extended Arabic-Indic ۱۲ invalid-number
    9223372036854775808 or -9223372036854775809 number-out-of-range

    The parser does not trim spaces or normalize digits. Runtime numeric text is separate from source notation and the source editor's digit conversion outside strings and comments. Keep the supplied text unchanged in this lesson. Reading-language and source-vocabulary changes do not translate input or an existing draft.

    Parsing and arithmetic have separate bounds checks. Both i64 endpoints parse successfully, but adding 1 to the maximum traps with integer-overflow. Use the small practice inputs for this retry program. See numeric text for the full conversion rules.

    Add two instead

    Change only the success branch's addition from + 1 to + 2. Submit 12x and then 41. Expected output is exactly Number: invalid-number\nNumber: 43\n, with status 0. The first failure still retries and never reads the numeric payload.

    Keep the retry template and add 2 to the successful integer instead of 1. Predict both replies.

    Hint: Change only the integer after + in the success branch. Keep the ok test, the failure branch and both flag values.

    Show worked answer

    The first reply 12x still returns invalid-number. The failure branch prints its error code and leaves pending true, so the loop asks again. The reply 41 succeeds; only that branch reads value and calculates 41 + 2 = 43. Formatting produces the text 43, the explicit newline ends the line, and pending becomes false. The entry returns status 0 without another read.

    Browser and native · ASPL 0.7.0 · English source

    Execution: Browser and native · ASPL 0.7.0

    Prerequisites:
    • Read the example explanation in its lesson before running.

    Input lines: ["12x","41"]

    fn start_main() -> i64 {
        var pending: bool = true;
        while pending {
            print("Number: ");
            let parsed: result<i64> = parse_i64(read_line());
            if (parsed::ok) {
                print(format_i64(parsed::value + 2) + "\n");
                pending = false;
            } else {
                print(parsed::error::code + "\n");
            }
        }
        return 0;
    }
    
    Download lesson-numeric-parsing-solution.aspl

    Expected outcomes
    {
      "browser": {
        "check": "success",
        "class": "success",
        "output": "Number: invalid-number\nNumber: 43\n",
        "processStatus": 0
      },
      "native": {
        "check": 0,
        "status": 0,
        "output": "Number: invalid-number\nNumber: 43\n"
      }
    }

    Check your understanding

    Explain why the failure branch can continue but cannot use the numeric payload. Predict the result of an empty reply, surrounding spaces and non-ASCII digits before trying them. Explain how out-of-range text differs from malformed text. No grade or progress is stored, and navigation stays open. Return to the Tutorial path or read the results reference.

    Browse pages