dazzl
v0.3.0
Published
The Dazzl compiler - compiles .daz files to JavaScript
Readme
Dazzl
A programming language that compiles to JavaScript. Dazzl takes inspiration from Elixir, Haskell, and Ruby to offer a concise, expressive syntax while targeting the JavaScript runtime.
Quick Start
npm install -g dazzl
dazzl myfile.daz
node myfile.jsDevelopment
git clone https://github.com/yourusername/dazzl.git
cd dazzl
npm install
npm test # compile examples/test.daz and run itLanguage Features
Variables
Bare assignment compiles to const by default:
name = "Dazzl"
x = 42Use let for mutable bindings:
let counter = 0Basic Types
# Strings
greeting = "Hello"
name = 'World'
# Numbers
count = 42
pi = 3.14
# Booleans
active = true
disabled = false
# Nil (alias for null)
empty = nil
also_null = null
not_defined = undefined
# Symbols (like Ruby/Elixir atoms)
status = :active
state = :pendingStrings and Interpolation
Double-quoted strings support interpolation with #{}:
name = "World"
"Hello #{name}!" # "Hello World!"
"2 + 2 = #{2 + 2}" # "2 + 2 = 4"
"Caps: #{String.upcase(name)}" # "Caps: WORLD"Multiline strings are supported:
message = "Line 1
Line 2
Line 3"Single-quoted strings are literal (no interpolation):
'Hello #{name}' # "Hello #{name}" (literal)Escape sequences: \\, \", \n, \t, \#
Arrays
empty = []
numbers = [1, 2, 3, 4, 5]
mixed = ["hello", 42, true, nil]Objects
Three styles that can be mixed:
# Colon style (JS-like)
{ name: "Alice", age: 30 }
# Hash rocket style (Ruby/Elixir-like)
{ name => "Alice", age => 30 }
# Shorthand style (ES6-like)
name = "Alice"
age = 30
{ name, age } # { name: "Alice", age: 30 }
# Mixed
{ name, city: "NYC", active => true }Operators
# Arithmetic
x + y # addition
x - y # subtraction
x * y # multiplication
x / y # division
x // y # integer division (Math.floor)
x % y # modulo
x ** y # exponentiation (right-associative)
# Unary
-x # negation
+x # unary plus
# Comparison (== and != compile to === and !==)
x == y
x != y
x < y
x <= y
x > y
x >= yNil Checking
Comparisons with nil/null/undefined use loose equality to catch both null and undefined:
x == nil # true if x is null OR undefined
x != nil # true if x is neitherFunctions
Four equivalent styles:
# Haskell-style
add x, y = x + y
# Math-style
add(x, y) = x + y
# Arrow-style
add: x, y -> x + y
# Arrow-style with parens
add: (x, y) -> x + yBlock bodies with implicit returns:
clamp: (val, min, max) -> {
if val < min { return min }
if val > max { return max }
val # implicit return
}Control Flow
# If/else (parentheses optional)
if x > 0 {
console.log("positive")
} else {
console.log("non-positive")
}
# Unless (negated if)
unless x == 0 {
console.log("not zero")
}
# Postfix conditionals
console.log("yes") if ready
console.log("no") unless valid
# Conditional expressions (like ternary)
sign = if x > 0 then "positive" else "negative"
grade = if score > 90 "A" else if score > 80 "B" else "C"Pipe Operator
Passes the left side as the first argument:
# These are equivalent:
console.log(add(result, 5))
result |> add(5) |> console.log
# Great with String module
" HELLO " |> String.trim |> String.downcase |> String.capitalizeRanges
r = [1..10]
Range.to_list(r) # [1, 2, 3, ..., 10]
Range.size(r) # 10
Range.member(r, 5) # trueComments
// JS-style comment
# Ruby-style comment
x = 42 // inline commentStandard Library
Modules are auto-imported when used.
Kernel Functions
Called directly (no module prefix):
# Math
abs(-5) # 5
div(10, 3) # 3
rem(10, 3) # 1
max(1, 2, 3) # 3
min(1, 2, 3) # 1
# Type checking
is_integer(42) # true
is_float(3.14) # true
is_binary("hi") # true (strings)
is_boolean(true) # true
is_sym(:ok) # true
is_list([1, 2]) # true
is_range([1..5]) # true
# Utilities
length("hello") # 5
to_string(42) # "42"
inspect(:ok) # ":ok"String Module
String.first("hello") # "h"
String.last("hello") # "o"
String.at("hello", -1) # "o"
String.capitalize("hELLO") # "Hello"
String.upcase("hello") # "HELLO"
String.downcase("HELLO") # "hello"
String.reverse("hello") # "olleh"
String.trim(" hi ") # "hi"
String.contains("hello", "ell") # true
String.starts_with("hello", "he") # true
String.ends_with("hello", "lo") # true
String.pad_leading("42", 5, "0") # "00042"
String.duplicate("na", 4) # "nananana"
String.split("a,b,c", ",") # ["a", "b", "c"]
String.replace("hello", "l", "L") # "heLlo"Integer Module
Integer.is_even(42) # true
Integer.is_odd(7) # true
Integer.digits(1234) # [1, 2, 3, 4]
Integer.digits(255, 16) # [15, 15]
Integer.undigits([1, 2, 3]) # 123
Integer.gcd(12, 8) # 4
Integer.floor_div(10, 3) # 3
Integer.mod(-5, 3) # 1 (Euclidean)
Integer.pow(2, 10) # 1024
Integer.to_string(255, 16) # "ff"How It Works
Dazzl compiles .daz files to .js files. Runtime modules are automatically:
- Detected by analyzing your code
- Imported at the top of the output
- Copied to
.dazzl_modules/next to your output file
Project Structure
src/
cli.js # CLI entry point
compiler.js # Orchestrates parsing and codegen
dazscript.pegjs # PEG grammar (Peggy)
codegen.js # AST to JavaScript generator
modules/
kernel.js # Kernel functions
range.js # Range module
integer.js # Integer module
str.js # String module
examples/
test.daz # Feature showcaseLicense
ISC
