PeaBox Lab › PeaLang › Learn › Chapter 2 › Lesson 4

Part I — Foundations · Chapter 2 · Lesson 4 of 5 · 7 min read

Lesson 4: Input, Output, String Interpolation, and Basic Diagnostics

Lesson level: Foundation

Purpose: Learn how PeaLang programs receive data through controlled boundaries, produce readable output, build interpolated messages, and interpret basic diagnostics.

Learning objectives

By the end of this lesson, you should be able to:

4.1 Where input comes from

PeaLang’s foundational language does not define a general interactive input statement. A standalone .pea example typically receives values by declaring them near the top of the file. An embedded or service-hosted program receives validated portable values through its host contract.

let customer_tier = "gold"
let service_impact = 6

These declarations simulate scenario input for a command-line lesson. In a real integration, the host should authenticate the caller, obtain external data, validate its shape and range, convert it into portable values, and only then invoke PeaLang logic.

4.2 Input is a boundary responsibility

External data is untrusted until validated. A PeaLang script should not assume that a number is within range, a required key exists, or a string belongs to an approved category merely because the host supplied it.

4.3 The print statement

The print statement evaluates one expression, converts the resulting value to text, and sends that text to the configured output channel.

print "PeaLang ready"
print total
print quantity * unit_price

Print is a statement rather than a function call in ordinary source syntax. Each print operation produces one output item through the runtime’s output handler.

4.4 Value-to-text behavior

ValuePrinted form
nullnull
truetrue
falsefalse
1212
12.512.5
12.012
[1, 2]JSON-like list text
{"status": "ready"}JSON-like map text

Strings print as their contents. Lists and maps are serialized into readable structured text. Do not depend on human-readable print output as a stable machine protocol unless the interface explicitly defines that contract.

4.5 Design output for the reader

A value without context is difficult to interpret:

print 240

A labeled message is better:

print "Response target: 240 minutes"

Good output identifies the item, value, units, and relevant reason. Stable reason codes are helpful for testing and audits, while explanatory text helps people understand the result.

4.6 Keep output safe

4.7 Interpolated strings

A quoted string containing an opening brace is parsed as an interpolated string. Text outside braces remains literal text; the content inside a matching brace pair must be a valid expression.

let name = "PeaLang"
print "Hello {name}"

The embedded expression is evaluated and converted with the same value-to-text rules used by print.

4.8 Interpolating expressions

Interpolation is not limited to one variable:

print "Subtotal: {quantity * unit_price}"
print 'Eligible: {customer_tier == "gold" and subtotal >= 30}'

For readability, complex calculations are often clearer as named values:

const subtotal = quantity * unit_price
print "Subtotal: {subtotal}"

The text inside braces is parsed as a single expression. Declarations or multiple statements are not valid inside interpolation.

4.9 Literal braces

Use doubled braces to emit literal brace characters:

print "Object syntax uses {{ and }}"

The doubled opening brace becomes one literal {, and the doubled closing brace becomes one literal }.

4.10 Interpolation errors

ErrorCause
Unterminated interpolation expressionAn opening brace has no later closing brace.
Empty interpolation expressionThe braces contain only whitespace.
Interpolation must contain an expressionThe content parses as something other than exactly one expression statement.
Undefined variableThe embedded expression references an unknown name.
Runtime expression errorThe embedded expression fails during evaluation.

Interpolation diagnostics point to the containing string’s source location. Inspect the brace pair and embedded expression first.

4.11 Runtime error format

A PeaLang runtime or parser error can include source identity, line, column, and message:

rules.pea:7:14: Undefined variable 'subtotl'. Did you mean 'subtotal'?

Read the fields from left to right:

A diagnostic location identifies where the runtime or parser associated the failure. The root cause may be immediately before it, especially for missing quotes, braces, or delimiters.

4.12 Checker diagnostic format

The source checker renders diagnostics in a structured form:

rules.pea:7:14: error P101: Undefined variable 'subtotl'

This adds severity and a diagnostic code. The code helps categorize recurring issues; the message and source context still determine the actual correction.

4.13 Diagnostic categories

StageTypical issue
LexingUnexpected character or unterminated quoted string
ParsingUnexpected token, missing delimiter, or invalid statement structure
Static checkingUndefined name, duplicate declaration, or suspicious source pattern
RuntimeDivision by zero, missing property, invalid index, const assignment, or failed call

Do not treat all failures as runtime problems. Identify the stage first, then use the tool appropriate to that stage.

4.14 A disciplined triage sequence

4.15 Common mistakes

4.16 Guided practice

4.17 Hands-on lab: Build a diagnostic report generator

Create case_report.pea using source-defined sample inputs for a support case. Calculate a priority condition and print:

Use both direct expression interpolation and named intermediate values. Keep the output free of secrets.

Then create temporary variants for:

Deliverable: The valid source and output, plus a diagnostic table containing failure stage, location, message, root cause, and minimal correction for each variant.

4.18 Output-review checklist

4.19 Review questions

4.20 Applied challenge

Turn the Chapter 1 case-priority output into a concise operational report. Include stable reason codes, clear units, masked identifiers, named boolean rule results, and a safe summary map. Create an accompanying diagnostic playbook for five likely source or runtime mistakes without exposing sensitive data.

Lesson summary

PeaLang receives real external input through controlled host boundaries, while foundational files use declared sample values. Print evaluates one expression and converts its result into readable text. Interpolated strings combine literal text with embedded expressions and support doubled braces for literal brace output. Useful diagnostics identify source locations and processing stages, while safe output remains labeled, deterministic, and free of secrets.

Lesson status: Approved.

← Previous Next →