Part I — Foundations · Chapter 2 · Lesson 5 of 5 · 5 min read
Chapter 2 Checkpoint: Unit Converter
Checkpoint level: Foundation
Purpose: Integrate lexical rules, values, declarations, assignment, expressions, precedence, interpolation, output design, and diagnostics in one conversion program.
Checkpoint outcomes
By completing this checkpoint, you will demonstrate that you can:
- define clear source-level inputs and immutable conversion constants;
- select and apply a conversion rule;
- use arithmetic and parentheses correctly;
- produce labeled, unit-aware output;
- validate supported modes and safe numeric ranges;
- test boundaries and reverse conversions;
- diagnose lexical, parse, static, and runtime failures; and
- describe how a host would make the converter truly interactive.
Project model
Because the foundational language has no general input statement, this command-line checkpoint is driven by editable source values. The learner changes conversion and input_value at the top of the file, then formats, checks, and runs it. A later host-integration version can obtain those values interactively and pass them through a validated boundary.
2C.1 Supported conversions
| Mode | Formula |
|---|---|
| c_to_f | fahrenheit = celsius * 9 / 5 + 32 |
| f_to_c | celsius = (fahrenheit - 32) * 5 / 9 |
| km_to_mi | miles = kilometres * 0.621371 |
| mi_to_km | kilometres = miles / 0.621371 |
| kg_to_lb | pounds = kilograms * 2.2046226218 |
| lb_to_kg | kilograms = pounds / 2.2046226218 |
2C.2 Required inputs and outputs
Declare these editable scenario inputs:
let conversion = "c_to_f"
let input_value = 25
The program must print:
- selected conversion mode;
- labeled input value and source unit;
- labeled result and target unit;
- a stable formula code;
- whether the request was valid; and
- a diagnostic message when the mode or input is invalid.
2C.3 Validation rules
- The conversion mode must match one of the six supported strings.
- Distance and mass input values must not be negative.
- Temperature values may be negative.
- The divisor constants must remain nonzero.
- An unsupported mode must not silently produce a default conversion.
- Output must remain labeled and must not contain secrets.
2C.4 Implementation constraints
- Use only constructs covered through Chapter 2.
- Use const for conversion factors and unit labels.
- Use let only for editable inputs and outputs that change by branch.
- Use ordered if/else-if/else branches.
- Use parentheses to make formula grouping explicit.
- Use interpolation for the final report.
- Do not use files, HTTP, process execution, or external packages.
2C.5 Acceptance criteria
- The source formats cleanly.
- The checker reports no error.
- Each supported mode returns the expected result.
- Unsupported modes return a clear invalid result.
- Negative distance or mass values are rejected.
- Negative temperature values are accepted.
- Every output value has an explicit unit.
- The program uses no unnecessary capability.
2C.6 Reference implementation
Attempt the checkpoint independently before comparing your source with this version.
const miles_per_km = 0.621371
const pounds_per_kg = 2.2046226218
let conversion = "c_to_f"
let input_value = 25
let result = 0
let source_unit = ""
let target_unit = ""
let formula_code = ""
let valid = true
let message = "Conversion complete"
if conversion == "c_to_f" {
source_unit = "°C"
target_unit = "°F"
formula_code = "C_TO_F"
result = (input_value * 9 / 5) + 32
} else if conversion == "f_to_c" {
source_unit = "°F"
target_unit = "°C"
formula_code = "F_TO_C"
result = (input_value - 32) * 5 / 9
} else if conversion == "km_to_mi" and input_value >= 0 {
source_unit = "km"
target_unit = "mi"
formula_code = "KM_TO_MI"
result = input_value * miles_per_km
} else if conversion == "mi_to_km" and input_value >= 0 {
source_unit = "mi"
target_unit = "km"
formula_code = "MI_TO_KM"
result = input_value / miles_per_km
} else if conversion == "kg_to_lb" and input_value >= 0 {
source_unit = "kg"
target_unit = "lb"
formula_code = "KG_TO_LB"
result = input_value * pounds_per_kg
} else if conversion == "lb_to_kg" and input_value >= 0 {
source_unit = "lb"
target_unit = "kg"
formula_code = "LB_TO_KG"
result = input_value / pounds_per_kg
} else {
valid = false
message = "Unsupported mode or invalid non-temperature input"
}
print "Mode: {conversion}"
print "Valid: {valid}"
print "Message: {message}"
if valid {
print "Input: {input_value} {source_unit}"
print "Result: {result} {target_unit}"
print "Formula: {formula_code}"
}
2C.7 Required test matrix
| Case | Mode | Input | Expected result |
|---|---|---|---|
| A | c_to_f | 0 | 32 °F |
| B | f_to_c | 32 | 0 °C |
| C | c_to_f | -40 | -40 °F |
| D | km_to_mi | 10 | 6.21371 mi |
| E | mi_to_km | 1 | Approximately 1.609344 km |
| F | kg_to_lb | 1 | 2.2046226218 lb |
| G | lb_to_kg | 2.2046226218 | Approximately 1 kg |
| H | km_to_mi | -1 | Invalid |
| I | unknown | 10 | Invalid |
Cases A–C test important temperature points, including the value where Celsius and Fahrenheit are equal. Cases H and I test validation.
2C.8 Round-trip tests
A round trip converts from one unit to another and back. Small floating-point differences may occur, so compare with an explicit tolerance rather than requiring exact decimal text.
- Convert 100 kilometres to miles, then convert the result back to kilometres.
- Convert 10 kilograms to pounds, then convert the result back to kilograms.
- Convert 20 °C to °F, then convert the result back to °C.
Record the original value, intermediate value, returned value, absolute difference, and acceptable tolerance.
2C.9 Validation workflow
- Change only conversion and input_value.
- Predict validity, units, formula code, and result.
- Run pea fmt unit_converter.pea.
- Run pea fmt --check unit_converter.pea.
- Run pea check unit_converter.pea.
- Run pea run unit_converter.pea.
- Compare actual output with the prediction.
- Record pass/fail and any floating-point tolerance.
2C.10 Deliberate diagnostic exercises
Create temporary variants that demonstrate:
- a misspelled factor name;
- assignment to a factor constant;
- a missing parenthesis in f_to_c;
- an empty interpolation expression;
- a zero divisor constant;
- an unsupported mode; and
- a negative mass or distance.
For each, record the processing stage, location, message or invalid output, root cause, and minimal correction.
2C.11 Submission package
- unit_converter.pea in canonical formatting;
- a prediction-and-results table for cases A–I;
- three round-trip test records;
- format-check, source-check, and execution evidence;
- a diagnostic table for all temporary variants;
- a short explanation of precedence in both temperature formulas; and
- a host-boundary note describing how validated interactive input would be supplied.
2C.12 Assessment rubric
| Area | Evidence of mastery |
|---|---|
| Correctness | All formulas and units are correct. |
| Validation | Unsupported modes and invalid non-temperature inputs are rejected. |
| Expression reasoning | Parentheses, precedence, and divisor safety are explained. |
| Declarations | Constants and mutable bindings are chosen deliberately. |
| Output | Messages are labeled, unit-aware, deterministic, and safe. |
| Diagnostics | Failures are classified and corrected minimally. |
| Testing | Boundary, invalid, and round-trip cases are covered. |
| Host design | Interactive input is assigned to a validated host boundary. |
Completion standard: Every required valid case passes, invalid cases are rejected clearly, and the learner can explain every expression and output field.
2C.13 Reflection questions
- Which formula was most sensitive to parentheses?
- Where did const improve safety?
- Which outputs are stable enough for automated checking?
- Why are reverse conversions approximate?
- What validation belongs in the host versus the script?
- Which diagnostic was easiest to classify, and why?
Chapter 2 summary
You can now describe PeaLang source lexically, declare and update bindings, reason about nested scope, evaluate expressions by precedence, use short-circuit logic, build interpolated output, and triage basic diagnostics. The unit-converter checkpoint integrates those skills in a complete program.
Transition to Chapter 3
Chapter 3 develops control flow in depth. The next lesson covers Boolean logic and conditional branches, including branch design, condition tables, boundary analysis, and readable policy ordering.
Checkpoint status: Approved.