Sema and RCompiler: A beginner-friendly language for interactive learning
Sema is a programming language built from the ground up to make learning code simple, intuitive, and enjoyable. Rather than starting with abstract instructions alone, Sema lets learners write code, see what happens, experiment with ideas, solve problems, and understand programming through direct interaction. The language is being developed with a long-term vision of powering interactive educational games where players learn programming by writing instructions, solving challenges, and exploring visual worlds.
RCompiler: the Rust-powered execution engine
The compiler and execution engine for Sema is written in Rust and called RCompiler. Its architecture separates language processing from visual presentation, allowing different games, simulations, and educational applications to use the same language engine without depending on a particular graphics framework. RCompiler processes Sema source code, checks its correctness, and evaluates valid programs. External applications can consume its structured execution events and decide how to represent them visually.
Language features designed for learning
Sema uses an instruction-oriented syntax that reads naturally. A simple program might set a score, display it, and show feedback based on its value:
set score to 100
show(score)
if score > 50 {
show("Great job!")
}
move(right, 3)
The language supports several categories of features that map directly to concepts beginners need:
- Variables can be created and updated using
set. Constants are declared withconstand cannot be modified after declaration. - Numbers, strings, booleans, and arrays are the basic data types.
- Arithmetic operators include
+,-,*, and/. - Comparison operators are
==,!=,<,>,≤, and≥. - Logical operators are
and,or, andnot.
Control flow for interactive programs
Sema provides multiple ways to control program flow. Conditional blocks use if and else:
set score to 75
if score > 50 {
show("You passed!")
} else {
show("Try again!")
}
Repeat loops execute a block a specified number of times:
repeat 4 {
move(right, 1)
}
While loops continue executing while a condition remains true:
set count to 0
while count < 5 {
set count to count + 1
}
Movement instructions are designed for interactive environments:
move(up, 2)
move(right, 4)
move(down, 1)
move(left, 3)
Functions, arrays, and reusable instructions
Functions allow reusable instructions to be defined and called. Functions support parameters and return values:
function greet(player_name) {
show(player_name)
}
greet("Welcome to Sema!")
function add_numbers(a, b) {
return a + b
}
set result to add_numbers(10, 20)
show(result)
Arrays store collections of values. Elements can be pushed, removed, and accessed by index:
set scores to [10, 20, 30]
push(scores, 40)
remove(scores, 1)
show(scores)
show(length(scores))
Comments and source structure
Sema supports single-line comments with // and multiline comments delimited by /c and /c:
// This is a comment
show("Hello!")
/c
This is a
multiline comment.
/c
Compilation and execution pipeline
RCompiler processes Sema programs through a clear pipeline. The lexer reads source code and converts it into tokens, recognizing identifiers, numbers, strings, operators, and keywords. Invalid characters and malformed strings are reported as lexical errors, and tokens retain source ranges for later diagnostics.
The parser organizes tokens into structured statements and expressions, checking that the source code follows Sema's grammatical rules. Syntax errors are reported when the structure is invalid, and parsed statements retain source-location information.
The semantic analyzer checks whether a syntactically valid program follows Sema's language rules. It detects problems such as undefined variables, duplicate constants, invalid constant modifications, undefined functions, duplicate function definitions, incorrect function argument counts, invalid built-in function usage, and return statements outside functions. Multiple errors can accumulate across a single analysis pass rather than stopping at the first one.
Module loading and diagnostics
RCompiler supports loading Sema source files with imported modules. The module loader resolves imported modules relative to the importing file, checks the importing file's directory and its modules subdirectory, detects circular module dependencies, prevents a module name from referring to multiple different files, qualifies imported function names to avoid naming conflicts, and preserves source information and file identity for diagnostics. Module files are currently restricted to imports and function definitions.
Structured diagnostics make compiler errors easier to understand and handle programmatically. A diagnostic contains a code, category, severity level, descriptive message, suggestion for correction, optional source file path, location, source range, and source-line excerpt. RCompiler distinguishes between four major error categories: lexical (invalid characters, strings, or comments), syntax (incorrectly structured instructions), semantic (violations of language rules), and runtime (problems encountered during execution).
The project defines specific semantic error codes: SEM001 for undefined variable, SEM002 for duplicate constant, SEM003 for constant name conflict, SEM004 for constant modification, SEM005 for undefined function, SEM006 for duplicate function, SEM007 for wrong function argument count, SEM008 for return outside function, SEM009 for invalid built-in usage, and SEM010 for constant declaration inside a function.
The sema-diagnostics executable accepts exactly one .sm source file and returns a JSON response. A successfully compiled program returns diagnostics as an empty array; compilation errors include the diagnostic code, category, severity, message, suggestion, file path, location, and source line excerpt.
Resource safeguards
RCompiler includes protections to prevent programs from consuming excessive resources. Current safeguards include a maximum execution-step limit, a maximum function-call depth, division-by-zero detection, array bounds validation, validation of array operations, and runtime error reporting with source locations. These help prevent infinite loops, excessive recursion, and invalid operations from proceeding unchecked.
Rust API for integration
RCompiler exposes a public Rust API for applications that need to compile, validate, load, and execute Sema programs. Main entry points include compile and compile_with_diagnostics for compilation without execution, compile_file and compile_file_with_diagnostics for loading and compiling source files with imported modules, and run and run_file for compiling and executing source code.
Adding RCompiler as a dependency in a Rust project is straightforward. In Cargo.toml, the dependency is specified with the path to the rcompiler directory. The API returns success statements or specific error types including lexical, syntax, semantic, and module errors, allowing applications to handle compilation results programmatically.
Sema and RCompiler together represent an approach to programming education that connects the instructions learners write with meaningful actions they can observe. By making programming concepts visible, understandable, and interactive, the project aims to lower the barrier to entry for new developers while providing a foundation that can support more advanced features over time.