Syntax Overview
In "GoMelan," the syntax follows a set of rules to define the structure of programs. This section provides an overview of the language's syntax, covering key elements and their usage.
Backus-Naur form (BNF)
<program> ::= <include-list> <function-declaration> <main-function>
<include-list> ::= <include-statement> | <include-statement> <include-list>
<include-statement> ::= "include" "*" "from" <module> ";" | "include" "(" <import-list> ")" "from" <module> ";"
<module> ::= <identifier>
<import-list> ::= <import-identifier> | <import-identifier> "," <import-list>
<import-identifier> ::= <identifier>
<internal-function> ::= "len()" | "strToInt()" | ...
<function-declaration> ::= "fn" <function-name> "(" <parameter-list> ")" "->" <return-type> <block>
<function-prototype> ::= "fn" <function-name> "(" <parameter-list> ")" "->" <return-type> ";"
<function-name> ::= <identifier>
<parameter-list> ::= <parameter> | <parameter> "," <parameter-list>
<parameter> ::= <identifier> ":" <type>
<return-type> ::= <type>
<type> ::= "Int" | "Char" | "String" | "Bool" | "[" <type> "]" | "Float" | "FnType(" <parameter-list> ")" "->" <return-statement>
<comment> ::= "//" | "/*...*/"
<block> ::= "{" <statement-list> "}"
<statement-list> ::= <statement> | <statement> <statement-list>
<statement> ::= <variable-declaration> | <assignment> | <for-loop> | <print-statement> | <return-statement>
<variable-declaration> ::= <identifier> ":" <type> "=" <expression> ";"
<assignment> ::= <identifier> "=" <expression> ";"
<condition> ::= "if" "(" <expression> ")" <block> | "if" "(" <expression> ")" <block> "else" <block>
<for-loop> ::= <for-loop-iter> | <for-loop-in>
<for-loop-in> ::= "for" "(" <identifier> "in" <expression> ")" <block>
<for-loop-iter> ::= "for" "(" <for-loop-initialization> ";" <for-loop-condition> ";" <for-loop-update> ")" <block>
<for-loop-initialization> ::= <variable-declaration> | <assignment> | <empty>
<for-loop-condition> ::= <expression>
<for-loop-update> ::= <assignment> | <empty>
<return-statement> ::= "return" <expression> ";"
<expression> ::= <term> | <term> <binary-operator> <expression>
<term> ::= <factor> | <factor> <binary-operator> <term>
<factor> ::= <identifier> | <literal> | "(" <expression> ")"
<literal> ::= <number> | <string> | "true" | "false"
<number> ::= <digit>+
<string> ::= '"' <characters> '"'
<characters> ::= <character> | <character> <characters>
<character> ::= (any valid character)
<identifier> ::= (valid identifier)
<binary-operator> ::= "+" | "-" | "*" | "/" | "%" | "==" | "!=" | "&&" | "||" | ...
<syntactic-sugar> ::= "++" | "--" | "+=" | "-=" | "*=" | "/=" | "%=" | ...
<digit> ::= '0' | '1' | '2' | ... | '9'
<empty> ::= epsilon
Details on the BNF
Program Structure
A "Gomelan" program consists of the following components:
<include-list>
: Importing modules and dependencies.<function-declaration>
: Defining functions.<main-function>
: The main entry point of the program.
Include Statements
You can include external modules using "include" statements:
<include-statement>
: Import a module or a list of imports.<module>
: The name of the module.<import-list>
: A list of import identifiers.
Functions
Functions in "Gomelan" are defined as follows:
<function-declaration>
: Declare a function with parameters and a return type.<function-name>
: Name of the function.<parameter-list>
: A list of parameters.<parameter>
: Define function parameters.<return-type>
: Specify the return type of the function.<internal-function>
: Functions to facilitate development
Data Types
"Gomelan" supports various data types:
<type>
: Can be "Int", "Char", "String", "Bool" or "FnType".
Blocks and Statements
Code blocks are enclosed within curly braces and contain statements:
<block>
: Defines a code block.<statement-list>
: A list of statements within a block.<statement>
: Represents individual statements.
Variables and Assignments
Variable declaration and assignments:
<variable-declaration>
: Declare and initialize variables.<assignment>
: Assign values to variables.
Control Flow
Control structures in "Gomelan":
<for-loop>
: For loop for iteration.<for-loop-iter>
: For loop with an incrementer
<condition>
: Conditional statements.<return-statement>
: Returning values from functions.
Expressions
Expressions in "Gomelan" follow standard mathematical and logical operators:
<expression>
: Represents expressions.<term>
and<factor>
: Building blocks for expressions.<binary-operator>
and<sintactic-sugar>
: Mathematical and logical operators.<comment>
: Write a comment
Literals
Literals represent constant values:
<number>
: Numeric values.<string>
: Text values."true" and "false": Boolean literals.
Identifiers
<identifier>
: User-defined names for variables, functions, and types.
Symbols
<digit>
: Numeric digits from '0' to '9'.<character>
: Any valid character.<binary-operator>
: Valid binary operators.
Epsilon
<empty>
: Represents an empty or optional element.
This syntax overview provides a solid foundation for understanding the structure and elements of "Gomelan" programs. Use these rules to create, modify, and comprehend "Gomelan" code effectively.
Last updated
Was this helpful?