Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for enums #197

Merged
merged 8 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/docs/language/enums.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: Enumerations
---

Enumerations in spice work like this:

```spice
type Fruit enum {
APPLE,
BANANA,
MANGO,
ORANGE
}

f<int> main() {
printf("Test: %d", Fruit.MANGO);
}
```

They can be defined only in the global scope. Spice will assign the unsigned integer values
0 to 3 to the items automatically.

Enum items can have values assigned to them. Here is an example:

```spice
type Vegetable enum {
CUCUMBER = 5,
TOMATO,
CARROT = 2,
POTATO
}
```

Spice will then assign 0 for `TOMATO` and 1 for `POTATO`.
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ nav:
- language/declaration-modifiers.md
- language/arrays.md
- language/pointers.md
- language/enums.md
- language/structs.md
- language/methods.md
- language/constructors-destructors.md
Expand Down
16 changes: 7 additions & 9 deletions media/test-project/os-test.spice
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ f<int> main() {
server.serve("/test", "Hello World!");
}*/

f<bool> f1() {
printf("F1 called.\n");
return false;
}

f<bool> f2() {
printf("F2 called.\n");
return false;
type TokenType enum {
IDENTIFIER,
DOT = 12,
COMMA,
SIZEOF = 0,
WS
}

f<int> main() {
printf("Result: %d", f1() ?: f2());
printf("%d\n", TokenType.DOT);
}
6 changes: 5 additions & 1 deletion src/Spice.g4
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
grammar Spice;

// Control structures
entry: (mainFunctionDef | functionDef | procedureDef | structDef | genericTypeDef | globalVarDef | importStmt | extDecl)*;
entry: (mainFunctionDef | functionDef | procedureDef | structDef | enumDef | genericTypeDef | globalVarDef | importStmt | extDecl)*;
mainFunctionDef: F LESS TYPE_INT GREATER MAIN LPAREN paramLst? RPAREN LBRACE stmtLst RBRACE;
functionDef: specifierLst? F LESS dataType GREATER (IDENTIFIER DOT)? IDENTIFIER (LESS typeLst GREATER)? LPAREN paramLst? RPAREN LBRACE stmtLst RBRACE;
procedureDef: specifierLst? P (IDENTIFIER DOT)? IDENTIFIER (LESS typeLst GREATER)? LPAREN paramLst? RPAREN LBRACE stmtLst RBRACE;
structDef: specifierLst? TYPE IDENTIFIER (LESS typeLst GREATER)? STRUCT LBRACE field* RBRACE;
enumDef: specifierLst? TYPE IDENTIFIER ENUM LBRACE enumItemLst RBRACE;
genericTypeDef: specifierLst? TYPE IDENTIFIER typeAltsLst SEMICOLON;
globalVarDef: specifierLst? dataType IDENTIFIER (ASSIGN value)? SEMICOLON;
extDecl: EXT (LESS dataType GREATER)? IDENTIFIER LPAREN (typeLst ELLIPSIS?)? RPAREN DLL? SEMICOLON;
Expand All @@ -28,6 +29,8 @@ typeLst: dataType (COMMA dataType)*;
typeAltsLst: dataType (BITWISE_OR dataType)*;
paramLst: declStmt (COMMA declStmt)*;
argLst: assignExpr (COMMA assignExpr)*;
enumItemLst: enumItem (COMMA enumItem)*;
enumItem: IDENTIFIER (ASSIGN INT_LIT)?;
field: specifierLst? dataType IDENTIFIER;
stmt: (declStmt | assignExpr | returnStmt | breakStmt | continueStmt) SEMICOLON;
declStmt: specifierLst? dataType IDENTIFIER (ASSIGN assignExpr)?;
Expand Down Expand Up @@ -109,6 +112,7 @@ RETURN: 'return';
AS: 'as';
STRUCT: 'struct';
TYPE: 'type';
ENUM: 'enum';
THREAD: 'thread';
UNSAFE: 'unsafe';
//NEW: 'new';
Expand Down
Loading