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:
- distinguish source-defined values from external input;
- explain why hosts should validate input before invocation;
- use print with literals, variables, and expressions;
- predict PeaLang’s text representation of common values;
- write and troubleshoot interpolated strings;
- emit literal braces inside interpolated text;
- read runtime and checker diagnostic formats; and
- design output that is useful without exposing secrets.
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.
- Define required fields and allowed value types.
- Reject or normalize malformed data before policy evaluation.
- Do not pass host-specific objects across the boundary.
- Do not embed secrets in source code.
- Keep validation and decision logic separately understandable.
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
| Value | Printed form |
|---|---|
| null | null |
| true | true |
| false | false |
| 12 | 12 |
| 12.5 | 12.5 |
| 12.0 | 12 |
| [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
- Do not print credentials, signing keys, tokens, or secret-provider values.
- Avoid unnecessary personal or sensitive data.
- Prefer identifiers or masked values when full data is not needed.
- Separate user-facing messages from protocol channels.
- Keep debugger and DAP transport output free of unrelated text.
- Use structured logging facilities for production observability rather than ad hoc sensitive print statements.
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
| Error | Cause |
|---|---|
| Unterminated interpolation expression | An opening brace has no later closing brace. |
| Empty interpolation expression | The braces contain only whitespace. |
| Interpolation must contain an expression | The content parses as something other than exactly one expression statement. |
| Undefined variable | The embedded expression references an unknown name. |
| Runtime expression error | The 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:
- rules.pea — source identity;
- 7 — line number;
- 14 — column number; and
- the remaining text — the error message.
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
| Stage | Typical issue |
|---|---|
| Lexing | Unexpected character or unterminated quoted string |
| Parsing | Unexpected token, missing delimiter, or invalid statement structure |
| Static checking | Undefined name, duplicate declaration, or suspicious source pattern |
| Runtime | Division 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
- Read the entire diagnostic once.
- Locate the source line and column.
- Classify the stage: lexical, parse, static, or runtime.
- Inspect the smallest surrounding expression or statement.
- Check the token immediately before the reported location.
- Make one minimal correction.
- Rerun formatting or checking before execution.
- Confirm that the original failure is gone without introducing another.
4.15 Common mistakes
- Treating source constants as trusted external input: real host data still requires validation.
- Printing bare values: add labels, units, and reason context.
- Printing secrets or sensitive records: minimize and mask output.
- Using complex interpolation unnecessarily: name the calculation first.
- Forgetting a closing interpolation brace: inspect the whole quoted string.
- Using empty braces: every interpolation pair must contain an expression.
- Using one brace when literal output is intended: double it.
- Fixing a runtime symptom before source errors: format and check first.
- Mixing protocol and human output: reserve standard streams when a protocol requires them.
4.16 Guided practice
- Rewrite print 240 as a labeled response-target message.
- Predict the output of interpolating null, true, 12.0, and [1, 2].
- Write an interpolation that calculates quantity * unit_price.
- Replace a complex interpolation with a named const and simpler message.
- Write a string that prints literal braces.
- Classify an unterminated interpolation as a parsing-stage failure.
- Break a diagnostic into source, line, column, severity/code if present, and message.
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:
- case identifier;
- masked customer reference;
- impact and affected-user values;
- boolean rule results;
- selected priority and response target;
- a reason code;
- a list or map summary; and
- one line containing literal braces.
Use both direct expression interpolation and named intermediate values. Keep the output free of secrets.
Then create temporary variants for:
- an undefined variable inside interpolation;
- an empty interpolation expression;
- an unterminated interpolation expression;
- a division-by-zero expression inside interpolation; and
- a malformed statement outside the string.
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
- Is every value labeled?
- Are units explicit?
- Can a reader identify why the decision occurred?
- Is the output deterministic for the same inputs?
- Is sensitive data omitted or masked?
- Is machine-consumed output separated from human prose?
- Can a test assert stable fields or reason codes?
4.19 Review questions
- How does a standalone lesson simulate input?
- What should a host do before passing external data to PeaLang?
- What does print do with its expression?
- How are null and booleans represented in output?
- Why should human-readable print output not automatically be treated as a stable protocol?
- What may appear inside interpolation braces?
- How are literal braces emitted?
- What causes an empty-interpolation diagnostic?
- How does a runtime error format differ from a checker diagnostic?
- Why should diagnostic triage identify the processing stage first?
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.