Skip to content

samualtnorman/sweet

Repository files navigation

Sweet

Influences

Notes

if

if condition
	body

is syntactic sugar for:

if condition then do
	body

enum

enum MyEnum
	Foo
	Bar
	Baz

is syntactic sugar for:

tag MyEnum: MyEnum.Foo ? MyEnum.Bar ? MyEnum.Baz

namespace MyEnum
	tag Foo
	tag Bar
	tag Baz

when

when one is Two { three, four }
	// ...

is syntactic sugar for:

if one is Two
	let { three, four } = one

	// ...

with

with one as Two { three, four }
	// ...

is syntactic sugar for:

block
	let { three, four } = one as Two

	// ...

Operators

+ (plus)

In sweet, the + is not the + operator from other languages. It takes take 2 numbers of any type and adds them together. However it prevents overflow by promoting the types before adding them. The type the + operator evaluates to is bits of the biggest input type plus 1.

e.g. (a as u4) + (b as u8) results in a u9 type.

- (minus)

Like sweet's + operator, the - operator also prevents overflow. Even if given 2 unsigned integer types, the result is always a signed integer type.

Grammar

Module = Statement<indent = 0>+

Statement \t{indent} Expression \n

Expression = Function | Identifier | Tag | Enum | UnaryExpression | BinaryExpression | Rement | Assignment | GetMember | VariableDeclaration | Object | Call | String | FunctionType | Import | Array | Do | If | While

DeclaredImport = declare import String as { DeclaredImportMember (, DeclaredImportMember)* }

DeclaredImportMember = ((Identifier | String) as)? Identifier : Expression

Array = [ (Expression (, Expression)*)? ]

Import = import String (as (Identifier | ImportDestructure))?

ImportDestructure = { (ImportDestructureMember (, ImportDestructureMember)*)? }

ImportDestructureMember = Identifier (as (Identifier | ImportDestructure))?

FunctionType = Expression -> Expression

String = " (\" | !")+ "

Call = Expression Expression

Object = { (ObjectMember (, ObjectMember)*)? }

ObjectMember = Identifier (: Expression)? (= Expression)?

VariableDeclaration = let (Identifier | Destructure) (= Expression)?

Destructure = { (DestructureMember (, DestructureMember)*)? }

DestructureMember = Identifier (as (Identifier | Destructure))? (= Expression)?

Rement = (Identifier | GetMember) RementOperator

RementOperator = ++ | -- | ++% | --%

UnaryExpression = UnaryOperator Expression

UnaryOperator = void | - | ~ | !

BinaryExpression = Expression BinaryOperator Expression

BinaryOperator = ArithmeticOperator | RelationalOperator | EqualityOperator | BitwiseShiftOperator | BinaryBitwiseOperator | BinaryLogicalOperator | ? | .. | to | as

ArithmeticOperator = + | - | / | * | % | ** | +% | -% | /% | *% | **%

RelationalOperator = is | < | > | <= | >=

EqualityOperator = == | !=

BitwiseShiftOperator = << | >> | <<%

BinaryBitwiseOperator = & | | | ^

BinaryLogicalOperator = && | || | ??

Assignment = (Identifier | GetMember | Destructure) AssignmentOperator Expression

GetMember = Expression . Identifier

AssignmentOperator = = | += | -= | /= | *= | %= | **= | +%= | -%= | /%= | *%= | **%= | <<= | >>= | <<%= | &= | |= | ^= | &&= | ||= | ??= | ..= | :=

Enum = enum Identifier \n (\t{(indent + 1)} Identifier (: Expression)? \n)+

While = while Expression \n Statement<indent = (indent + 1)>+

If = if Expression (then Expression | \n Statement<indent = (indent + 1)>+) (else (Expression | \n Statement<indent = (indent + 1)>+))?

Do = do \n Statement<indent = (indent + 1)>+

Tag = tag Identifier (: Expression)?

Function = function Identifier (Identifier | Object) (: Expression)? (-> Expression)? \n Statement<indent = (indent + 1)>+

Identifier = (a-z | A-Z | _) (a-z | A-Z | _ | 0-9)*

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published