A friendly little language for you and me.
Ghost is the language I wish we had.
There are so many newer programming languages that have brilliant ideas inside them, but I think they have failed to make them feel familiar to programmers.
Ghost steals as much of its design as possible from other programming languages. If someone else did it really well, why do it any differently? As such most of Ghost won't feel new. If you've used some of these languages before you will probably recognize features of Ghost.
There is no compiler or any tools for Ghost right now, I hope to someday work on some. But for now, this is just an exercise in programming language design.
Ghost's syntax should feel light. There are no semicons, comments are a single
character (#
), syntax avoid too many characters.
But it also avoids having "optional" syntax like optional parenthesis or curly braces. A lot of this encourages a more "vertical" coding style.
Places where syntax could go multiple different ways, I've just picked what seems to be most popular. That way it should feel familiar to lots of developers.
# inline comments only
# combine to make multiline
let name = expression
let missing = null
let positive = true
let negative = false
let plain = 'hello world'
let interpolated = "hello {target}"
let escapes = 'hello \'world\''
let multiline =
'''
The quick brown fox # comment
jumps over the lazy dog \# not comment
'''
let multilineWithInterpolation =
"""
The quick {color} fox # comment
{action} over the lazy dog \# not comment
"""
let multilineWithNewlines =
'''
This is the first line
This is the second line\
This is still the second line
'''
let multilineWithIndentation =
'''
This is indented by 0 spaces.
This is indented by 2 spaces.
This is indented by 4 spaces.
'''
let float = 4.14
let float32 = 4.14f
let decimal = 0.15d
let bigint = 9999i
let largeNumber = 98_521_391_124i
let regex = /^one|^two|^three|^orfour/i # equivalent to below
let regexMultiline = ///
^ one | # all whitespace
^ two | # and comments
^ three | # will be ignored
^ or four # to make it readable
///i
let range = 0..3 # 0,1,2
let rangeInclusive = 0...3 # 0,1,2,3
let rangeExpr = 0..num # 0 to whatever `num` is
let reverseWithNegatives = 2..-3 # 2,1,0,-1,-2
let symbol1 = :name # symbols are global
let symbol2 = :name # both of these are equal
let list = [1, 2, 3]
let trailingCommas = [
1,
2,
3,
]
let combineList = [1, ..list, 3] # fast
let combineIter = [1, ...iter, 3] # slow
let getterIndex = list[3]
let getterRange = list[0..3]
let array = [|1, 2, 3|]
let trailingCommas = [|
1,
2,
3,
|]
let getterIndex = array[3]
let getterIndexNegative = array[-3]
let getterRange = array[0..3]
let three = :three
let record = {
one: 1,
two: 2,
[three]: 3,
}
let one = record.one
let two = record[:two]
let three = record[three]
let newRecord = {
one: 'default',
...record,
three: 'new value',
}
let map = {=
'key1' = 'value1',
'key2' = 'value2',
3.1415 = 'π'
=}
let set = {-
'one',
'two',
'three',
-}
# Arithmetic
let addition = a + b
let subtraction = a - b
let division = a / b
let multiplication = a * b
let remainder = a % b
let exponentiation = a ** b
let negation = -a
# Bitwise
let and = a & b
let or = a | b
let xor = a ^ b
let not = ~a
let leftShift = a << b
let signPropRightShift = a >> b
let zeroFillRightShift = a >>> b
# Comparison
let equality = a == b
let referentialEquality = a === b
let lessThan = a < b
let lessThan2 = a < b < c # equivalent to `a < b && b < c`, except `b` is only evaluated once
let lte = a <= b
let lte2 = a <= b <= c
let lessThan3 = a <= b < c
let greaterThan = a > b
let greaterThan2 = a > b > c
let gte = a >= b
let gte2 = a >= b >= c
let greaterThan3 = a >= b > c
# Logical
let and = expr && expr
let or = expr || expr
let not = !expr
# Grouping
let either = (a && b) || (c && d)
if (condition) { doSomethingIfTruthy() }
if (condition) {
doSomethingIfTruthy()
} else {
doSomethingElse()
}
if (condition) {
doSomethingIfTruthy()
} else if (condition2) {
doSomethingElseIf2Truthy()
} else {
doSomethingElse()
}
let result = if (n == 0) {
'none'
} else if (n == 1) {
'one'
} else {
'many'
}
let [one, two, ...rest] = [1, 2, 3, 4]
# one = 1
# two = 2
# rest = [3, 4]
let [|one, two, ...rest|] = [|1, 2, 3, 4|]
# one = 1
# two = 2
# rest = [|3, 4|]
let {one, two, ...rest} = {one: 1, two: 2, three: 3, four: 4}
# one = 1
# two = 2
# rest = {three: 3, four: 4}
let {one as uno, two as dos, ...rest as resto} = {one: 1, two: 2, three: 3, four: 4}
# uno = 1
# dos = 2
# resto = {three: 3, four: 4}
value is any
value is true # assert any static type
value is false
value is Number
value is Number.Float # return true or false if it matches
value is { prop: any }
value is { prop: Number }
let result = match (value) {
is true { 'true' }
is false { 'false' }
else { 'other' }
}
throw Error('message')
throw Error('message', reason)
let result = try {
doSomething()
} catch (err is SpecialError) {
doSomethingSpecial()
} catch (err) {
doSomethingElse()
} finally {
doSomethingAlways()
}
let doSomething = fn () {
let myResult = effect 'myEffect'
}
let result = try {
fn()
} catch (myEffect is 'myEffect') {
resume 'myResult'
}
let inner = fn () {
log('inner start')
let result = effect 'myEffect'
log('inner end', result)
result
}
let outer = fn () {
log('outer start')
let result = inner()
log('outer end', result)
result
}
let finalResult = try {
log('try start')
let result = outer()
log('try end', result)
result
} catch (effect is 'myEffect') {
log('catch start', effect)
let result = resume 'myEffectHandlerResult'
log('catch end', result)
}
# log: try start
# log: outer start
# log: inner start
# log: catch start 'myEffect'
# log: inner end 'myEffectHandlerResult'
# log: outer end 'myEffectHandlerResult'
# log: try end 'myEffectHandlerResult'
# log: catch end 'myEffectHandlerResult'
finalResult == 'myEffectHandlerResult'
for (iterable as item) {
if (item.shouldBeSkipped) { continue }
if (item.shouldEndTheLoop) { break }
doSomething()
}
while (condition) {
onlyRunsIfConditionIsTrue()
repeatsAsLongAsItRemainsTrue()
}
do {
runsOnceImmediatelyRegardlessIfConditionIsTrue()
repeatsAsLongAsItRemainsTrue()
} while (condition)
loop {
if (condition1) { break }
if (condition2) { continue }
doSomethingInfinitelyUntilBreak()
}
let add = fn (a, b) { a + b }
let add = fn (a, b) {
let addend = a
let augend = b
a + b
}
let result = add(400, 20)
let divide = fn (dividend, divisor) {
dividend / divisor
}
# All of these are equivalent:
divide(400, 20)
divide(dividend: 400, divisor: 20)
divide(divisor: 20, dividend: 400)
divide(dividend: 400, 20)
divide(400, divisor: 20)
divide(divisor: 20, 400)
# Compiler Errors: Cannot pass multiple values to the same parameter
divide(dividend: 20, dividend: 400)
divide(20, dividend: 400)
let doubles = fn (range) iter {
for (range as index) {
yield index * 2
}
}
let value = do {
let a = 42
let b = 10
a * b
}
let results =
|> books
|> Iter.filter(^^, fn (book) { book.popularity > 0.8 })
|> Iter.map(^^, fn (book) { Http.request(:get, book.url) })
# Equivalent code without pipelines:
let filtered = Iter.filter(books, fn (book) { book.popularity > 0.8 })
let results = Iter.map(filtered, fn (book) { Http.request(:get, book.url) })
let append = fn (fileName, buffer) {
let file = use File.open(fileName)
File.append(file, buffer)
}
# Effectively:
let append = fn (fileName, buffer) {
let file = File.open(fileName)
try {
let result = File.append(file, buffer)
} finally {
if (file) {
file.dispose()
}
}
result
}
let append = fn (fileName, buffer) {
let file = use File.open(fileName)
File.append(file, buffer)
file.dispose()
}
let _ = 'ignore me'
let [_, _, three] = [1, 2, 3]
let {a as _, b as _, ...rest as _} = { a: 1, b: 2, c: 3, d: 4 }
doSomething(fn (_, _, value) {
value
})
log(_)
# SyntaxError: "Junk" bindings (_) are not valid
# identifiers and cannot be referenced. Give your
# binding a name instead.
let MyComponent = fn (props) {
let {prop, items} = props
<OtherComponent { prop, bar: true }>
let target = 'world'
<:h1>"hello {target}"</:h1>
<:ul>
for (items as item) {
<:li { key: item.id }>item.name</:li>
}
</:ul>
</OtherComponent>
}
let fn = fn () {
# block
}
for (iter as item) {
# block
}
if (cond) {
# block
} else if (cond) {
# block
} else {
# block
}
try {
# block
} catch (err) {
# block
} finally {
# block
}
let a = 1
if (cond) {
let a = 2
let b = 3
log(a) # > 2
}
log(a) # > 1
log(b) # Error! There's no variable named "b" in this scope!
import Time
Time.Instant()
import Time as MyTime
MyTime.Instant()
import Math as { cos, PI }
cos(PI)
import ./utils/currency
currency.convert(42, :usd, :aud)
import ../lib/utils/i18n as { t }
t('Hello $1', 'World')
export let add = fn (a, b) { a + b }
export let PI = 3.14
type MyType = type
type MyType = any
type MyType = null
type MyType = Boolean
type MyType = true
type MyType = false
type MyType = String
type MyType = 'string'
type MyType = Number
type MyType = Number.Float
type MyType = Regex
type MyType = Range
type MyType = Symbol
type MyType = :symbol
type MyType = List<String>
type MyType = Array<Boolean>
type MyType = Map<Number, Regex>
type MyType = Set<Range>
type MyType = Iter<String>
type MyType = { prop: String, :symbol: Number }
type MyType = { prop?: String }
type MyType = { ...OtherRecordType }
type MyType = Boolean?
type MyType = Boolean | null # same
type MyType = fn (param: String): Number
type MyType = fn (param: String): Iter<Number> # fn () iter {}
type MyType<T> = Array<T>
type MyType = fn <T> (param: T): T
type MyType = String | Boolean | Range
type MyType =
| String
| Boolean
| Range
let value: Boolean = true
let value: Boolean | String = 'cool'
let fn = fn (param1: String, param2: Number) {
# ...
}
let fn = fn (...rest: Array<String>) {
# ...
}
let fn = fn (): String {
'nice'
}
let fn = fn (): Iter<String> iter {
yield 'good'
}