prism-lang
v0.1.6
Published
Prism Programming Language Transpiler
Downloads
844
Readme
Prism
Prism is a small, expressive language designed to transpile into TypeScript or JavaScript. It supports functions, classes, control flow, imports, typed variables, arrays, objects, and error handling.
Status
Prism is a work in progress. The syntax below is based on the current tokenizer and AST structure.
Features
- Typed variables and function signatures
- Mutable and immutable bindings
- Functions and methods
- Classes with public and private members
- Conditionals
- Pattern matching
whileandforloops- Arrays and objects
- Imports
newexpressionstry/catchandthrow- Unary, binary, compound, and nullish operators
Installation
npm install
npm run buildCLI
Prism includes a command-line tool.
To use it, first use:
npm linkprism compile file.prism
prism run file.prism
prism check file.prismOptions
--js Emit plain JavaScript instead of typed output
--ast Print the AST as JSON
--out <file> Write output to a file
--help Show helpBasic Syntax
Variables
Use mut for mutable variables and final for immutable variables.
final name: string = "Prism"
mut count: int = 0Type annotations are optional when the compiler can infer them.
mut value = 10Functions
fn greet(name: string): string {
return "Hello, " + name
}Functions can also return nothing:
fn logMessage(message: string): void {
print(message)
}Conditionals
if count > 10 {
print("high")
} else {
print("low")
}While Loops
while count < 10 {
count += 1
}For Loops
for item in items {
print(item)
}Match Statements
match status {
"ok" => print("success")
"error" => print("failed")
default => print("unknown")
}Functions and Calls
fn add(a: int, b: int): int {
return a + b
}
print(add(2, 3))Classes
Prism supports classes with public and private members.
class User {
pub name: string
priv token: string
pub fn getName(): string {
return this.name
}
priv fn setToken(token: string): void {
this.token = token
}
}Creating Instances
mut user = new User("Alice")Imports
use fs
use { readFile, writeFile } from "fs"
use path from "path"Arrays
mut numbers = [1, 2, 3]
print(numbers[0])Objects
mut person = {
name: "Alice",
age: 12
}Operators
Prism supports:
- Arithmetic:
+,-,*,/ - Comparison:
==,!=,>,<,>=,<= - Logical:
!,&&,|| - Nullish:
?? - Assignment:
=,+=,-=,*=,/= - Arrow syntax:
->,=>
Literals
true
false
null
"hello"
123
12.5Types
Prism currently recognizes these types:
stringintfloatboolvoidany
Error Handling
try {
riskyCall()
} catch err {
print(err)
}Throw an error:
throw "Something went wrong"AST Overview
The parser produces an AST with nodes such as:
ProgramVariableDeclarationFunctionDeclarationClassDeclarationMethodDeclarationCallExpressionMethodCallNewExpressionIfStatementMatchStatementWhileStatementForStatementTryCatchStatementReturnStatementAssignmentCompoundAssignmentArrayExpressionObjectExpression
Example Program
use { readFile } from "fs"
final appName: string = "Prism"
mut counter: int = 0
fn increment(value: int): int {
return value + 1
}
while counter < 3 {
counter += 1
}
if counter == 3 {
print(appName)
} else {
print("not ready")
}Project Structure
A typical Prism project may include:
src/
lexer.ts
parser.ts
index.ts
cli.ts
examples/
example.prismNotes
- Prism is transpiled, not interpreted directly.
- The exact syntax may evolve as the language grows.
- Some built-in function names such as
printmay depend on your runtime or transpiler output.
License
MIT
