From eabb8038321ffb58b0e7b61a16d8a196aa0eeecf Mon Sep 17 00:00:00 2001 From: RedoC-github Date: Sat, 30 Sep 2023 01:31:55 +0900 Subject: [PATCH 1/5] update grammar.md (temp) --- doc/grammar.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/doc/grammar.md b/doc/grammar.md index 848ec40..64e6749 100644 --- a/doc/grammar.md +++ b/doc/grammar.md @@ -20,6 +20,7 @@ - [Boolean Literal] - [Types] - [Variables] +- [Objects] - [Expressions] - [Blocks] - [Declarations] @@ -143,7 +144,7 @@ The following character sequences represent operators (including assignment oper + && == != ( ) - || < <= [ ] * > >= / : = - ; % ! +~ ; % ! ``` ### Integer Literal @@ -270,6 +271,28 @@ int ooo = -1 string poo = "result: " + tostring(woo + ooo) string soo = poo ``` +## Objects + +:warning: ***WARNING! OBJECT IS ON DEVELOPMENT SO THE SPEC CAN BE CHANGED ANY TIME*** +An object is a general storage for various data structures. Objects store data in their *container*. + +### Object Types +Some types, called *object types*, can be used to represent what the object stores in their container. Three types of object types can be used in Wopslang v0.2: *Array*, *Func*, and *Object*. *Array* and *Func* are the predefined object types and represent a list of values and a function, respectively. You can define your objects by using *Object* object type. + +```ebnf +ObjectType = ("Array" | "Func" | "Object") "<" { Type } ">" +``` +### Defining Object +NOT CONFIRMED YET. STAY TUNED. +### Declaring Object +There are two types of object which can be used in Wopslang v0.2: *constant object, modifiable object*. You can declare constant object as adding const keyword in object declare expression. Also, you should add initial value to declare constant object. Interpreter can emit the error if initial value is not compatible with the container of the object. + +```go +/* WARNING: NOT CONFIRMED YET*/ + +Array arr <- {1 ~ 5} +Func sum <- {int a, int b, { return a+b }} +``` ## Expressions @@ -519,6 +542,7 @@ Redirect to [here][ext-link-1] [Lexical Elements]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#lexical-elements [Types]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#types [Variables]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#variables +[Objects]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#objects [Expressions]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#expressions [Statements]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#statements [If Statement]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#if-statement From 97e4645ede8db185d30840dab6ac398cf37784f0 Mon Sep 17 00:00:00 2001 From: RedoC Date: Mon, 2 Oct 2023 00:13:25 +0900 Subject: [PATCH 2/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6268550..837e536 100644 --- a/README.md +++ b/README.md @@ -67,4 +67,4 @@ Special Thanks to @jhhan128 ![Wopslang Logo](https://emoji.slack-edge.com/T01MFM2TJ07/wopsfull/7fe35e7cbecd2d4d.png) -2022, Wops Team +2023, Wops Team From 0779f10bf9446d1fec93fce524b3cb0ac77af4e4 Mon Sep 17 00:00:00 2001 From: RedoC-github Date: Mon, 2 Oct 2023 10:58:53 +0900 Subject: [PATCH 3/5] update grammar.md (temp) --- doc/grammar.md | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/doc/grammar.md b/doc/grammar.md index 64e6749..7673cb3 100644 --- a/doc/grammar.md +++ b/doc/grammar.md @@ -21,6 +21,9 @@ - [Types] - [Variables] - [Objects] + - [Object Types] + - [Defining Object] + - [Declaring Object] - [Expressions] - [Blocks] - [Declarations] @@ -144,7 +147,7 @@ The following character sequences represent operators (including assignment oper + && == != ( ) - || < <= [ ] * > >= / : = -~ ; % ! +~ ; % ! <- ``` ### Integer Literal @@ -273,17 +276,17 @@ string soo = poo ``` ## Objects -:warning: ***WARNING! OBJECT IS ON DEVELOPMENT SO THE SPEC CAN BE CHANGED ANY TIME*** An object is a general storage for various data structures. Objects store data in their *container*. ### Object Types Some types, called *object types*, can be used to represent what the object stores in their container. Three types of object types can be used in Wopslang v0.2: *Array*, *Func*, and *Object*. *Array* and *Func* are the predefined object types and represent a list of values and a function, respectively. You can define your objects by using *Object* object type. ```ebnf -ObjectType = ("Array" | "Func" | "Object") "<" { Type } ">" +ObjectType = ("Array" | "Func" | "Object") TypeBlock ``` ### Defining Object NOT CONFIRMED YET. STAY TUNED. + ### Declaring Object There are two types of object which can be used in Wopslang v0.2: *constant object, modifiable object*. You can declare constant object as adding const keyword in object declare expression. Also, you should add initial value to declare constant object. Interpreter can emit the error if initial value is not compatible with the container of the object. @@ -298,12 +301,14 @@ Func sum <- {int a, int b, { return a+b }} ### Blocks -A block is an empty sequence of declarations and statements within matching brace({,})s. +A block is an empty sequence of declarations and statements within : and ;. ```ebnf -Block = ":" Statements ";" . -IfBlock = "?" Statements ";" . -ForBlock = "$" Statements ";" . +Block = ":" [ Statements ] ";" . +IfBlock = "?" [ Statements ] ";" . +ForBlock = "$" [ Statements ] ";" . +ObjectBlock = "{" { ObjectStmt ","} [ ObjectStmt ] "}" . +TypeBlock = "<" { ObjectStmt } ">" . Statements = { Statement } . ``` @@ -315,9 +320,11 @@ A declaration bind *identifiers* and *value* to a constant or [variable][Variabl Every identifier in a program must be declared, and no identifier may be declared twice in the same block. See [Variables] to get more information. ```ebnf -Declaration = ConstDel | VarDel . -ConstDel = "const" Type identifiers "=" Expression . +Declaration = ConstVarDel | VarDel | ConstObjDel | ObjDel . +ConstVarDel = "const" Type identifiers "=" Expression . VarDel = Type identifiers [ "=" Expression ] . +ConstObjDel = "const" ObjectType identifiers ( "<-" ObjectBlock | "=" Expression ). +ObjDel = ObjectType identifiers [ "<-" ObjectBlock | "=" Expression ] . ``` To find syntax defination of `Expression`, see [Operators] for more. @@ -392,9 +399,7 @@ Each operator has a different priority for parsing. For instance, unary operator |4|`+`, `-`| |3|`==`, `!=`, `<`, `<=`, `>`, `>=`| |2|`&&`| -|1|binary_op OR | - -> binary_op OR : `||` +|1|`\|\|`| The leftmost operator in the same priority has a higher priority. For instance, a+b-c is the same with (a+b)-c. @@ -424,6 +429,7 @@ Statement = BreakStmt | ContinueStmt | IfStmt | ForStmt . +ObjectStmt = ObjectBlock | Statement . SimpleStmt = Blank | Expression | Assignment . ``` @@ -535,8 +541,7 @@ ContinueStmt = "continue" . Redirect to [here][ext-link-1] - -[Introduction]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#introduction +[Introduction]: #introduction [Notation]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#notation [Source Code Representation]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#source-code-representation [Lexical Elements]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#lexical-elements From 02b295230dd17ae668a4f6a6dae2ae5432a6360f Mon Sep 17 00:00:00 2001 From: RedoC-github Date: Tue, 3 Oct 2023 02:03:33 +0900 Subject: [PATCH 4/5] update grammar.md (temp) --- doc/grammar.md | 149 +++++++++++++++++++++++++++++++------------------ 1 file changed, 96 insertions(+), 53 deletions(-) diff --git a/doc/grammar.md b/doc/grammar.md index 7673cb3..878d3cc 100644 --- a/doc/grammar.md +++ b/doc/grammar.md @@ -20,6 +20,7 @@ - [Boolean Literal] - [Types] - [Variables] +- [Containers] - [Objects] - [Object Types] - [Defining Object] @@ -147,7 +148,7 @@ The following character sequences represent operators (including assignment oper + && == != ( ) - || < <= [ ] * > >= / : = -~ ; % ! <- +~ ; % ! <- , ``` ### Integer Literal @@ -274,6 +275,30 @@ int ooo = -1 string poo = "result: " + tostring(woo + ooo) string soo = poo ``` + +## Containers + +A container is an array-structure storage for holding multiple values. +There is no need for every element to have the same type, so the container has no specific type. +Values in the container can be a variable, a container, or even statements. +Containers can be represented like following code. + +```go +{1, 2, {3, 4}, 5} +``` + +Because objects use containers to store their data, you can assign a container to an object instead of assigning an object to it. +You can also use a container instantly in for statement. +However, you can't declare a container directly and use it. + +```go +Array arr <- {1 ~ 9} +for i in arr $ + for j in {1 ~ 9} $ + out(i, "*", j, "=", i*j) + ; +; +``` ## Objects An object is a general storage for various data structures. Objects store data in their *container*. @@ -282,19 +307,33 @@ An object is a general storage for various data structures. Objects store data i Some types, called *object types*, can be used to represent what the object stores in their container. Three types of object types can be used in Wopslang v0.2: *Array*, *Func*, and *Object*. *Array* and *Func* are the predefined object types and represent a list of values and a function, respectively. You can define your objects by using *Object* object type. ```ebnf -ObjectType = ("Array" | "Func" | "Object") TypeBlock +ObjectType = ("Array" | "Func" | "Object") ObjType +``` + +If the object type has argument type, you can add argument types like + +```go + // {} is a type container ``` +For example, +```go +Array arr_dim1 // 1-dim array +Array arr_dim2 // 2-dim array +Func<{int, int}, int> fun_2_int_arg_1_int_ret // (int, int) -> int +``` + ### Defining Object NOT CONFIRMED YET. STAY TUNED. ### Declaring Object -There are two types of object which can be used in Wopslang v0.2: *constant object, modifiable object*. You can declare constant object as adding const keyword in object declare expression. Also, you should add initial value to declare constant object. Interpreter can emit the error if initial value is not compatible with the container of the object. +There are two ways of assigning a value to an object used in Wopslang v0.2: *Assigning an object directly*, and *Assigning a container into the object's container*. +The former uses the regular operator *=* and works the same with assigning variables. +Latter uses the special operator *<-* only available in Object. +As for adding const keyword, of course, you can declare a constant object with a mandatory initial value. ```go -/* WARNING: NOT CONFIRMED YET*/ - Array arr <- {1 ~ 5} -Func sum <- {int a, int b, { return a+b }} +Func<{int, int} int> sum <- {int a, int b, { return a+b }} ``` ## Expressions @@ -307,8 +346,6 @@ A block is an empty sequence of declarations and statements within : and ;. Block = ":" [ Statements ] ";" . IfBlock = "?" [ Statements ] ";" . ForBlock = "$" [ Statements ] ";" . -ObjectBlock = "{" { ObjectStmt ","} [ ObjectStmt ] "}" . -TypeBlock = "<" { ObjectStmt } ">" . Statements = { Statement } . ``` @@ -323,8 +360,8 @@ Every identifier in a program must be declared, and no identifier may be declare Declaration = ConstVarDel | VarDel | ConstObjDel | ObjDel . ConstVarDel = "const" Type identifiers "=" Expression . VarDel = Type identifiers [ "=" Expression ] . -ConstObjDel = "const" ObjectType identifiers ( "<-" ObjectBlock | "=" Expression ). -ObjDel = ObjectType identifiers [ "<-" ObjectBlock | "=" Expression ] . +ConstObjDel = "const" ObjectType identifiers ( "<-" Container | "=" Expression ) . +ObjDel = ObjectType identifiers [ "<-" Container | "=" Expression ] . ``` To find syntax defination of `Expression`, see [Operators] for more. @@ -334,9 +371,11 @@ To find syntax defination of `Expression`, see [Operators] for more. Operands represent the elementary values in an expression. It can be a *literal*, a *function*, or a *variable*. ```ebnf -Operand = Literal | OpndName | "(" Expression ")" . +Operand = Literal | OpndName | "(" Expression ")" | Container | ObjType. Literal = integer_lit | float_lit | rune_lit | string_lit . OpndName = identifier . +Container = "{" { (Container | Statement) "," } [Container | Statement] "}" . +ObjType = "<" {Container | Type} ">" . ``` Also, there are some unit expression groups: @@ -355,11 +394,12 @@ foo boo (3 + 0.25) out("Hello, World!\n", "Nice to meet you :D") +{"this", "is", {"a", "container"}} ``` ### Calls -> We'll support user-made function very soon (maybe next version). Stay tuned. +> Defining function will be supported with Func object. Here is the basic formation of function: @@ -429,7 +469,6 @@ Statement = BreakStmt | ContinueStmt | IfStmt | ForStmt . -ObjectStmt = ObjectBlock | Statement . SimpleStmt = Blank | Expression | Assignment . ``` @@ -446,15 +485,18 @@ Blank = . The assignment statement assigns a value to the specified variable. ```ebnf -Assignment = identifiers "=" Expression . +Assignment = identifiers "=" Expression | identifiers "<-" Container . ``` -Left-side operand should be lvalue. Also, left-side and right-side operand should share matching type(same type, double-int, ...). +For `=` assignment, Left-side operand should be lvalue. +Also, left-side and right-side operand should share matching type(same type, double-int, ...). +For `<-` assignment, right-side should be a container matching with left-side operand's type. ```go a = "Hello, " + in() -b = 30 * (50 / 27) <- b = 30 +b = 30 * (50 / 27) // b = 30 c = 0.5 - 1.3 +d <- {1 ~ 5} // d = {1, 2, 3, 4, 5} ``` ### If Statement @@ -542,44 +584,45 @@ Redirect to [here][ext-link-1] [Introduction]: #introduction -[Notation]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#notation -[Source Code Representation]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#source-code-representation -[Lexical Elements]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#lexical-elements -[Types]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#types -[Variables]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#variables -[Objects]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#objects -[Expressions]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#expressions -[Statements]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#statements -[If Statement]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#if-statement -[For Statement]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#for-statement -[Builtin functions]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#builtin-functions -[Tokens]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#tokens -[Predeclared Identifiers]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#predeclared-identifiers +[Notation]: #notation +[Source Code Representation]: #source-code-representation +[Lexical Elements]: #lexical-elements +[Types]: #types +[Variables]: #variables +[Containers]: #containers +[Objects]: #objects +[Expressions]: #expressions +[Statements]: #statements +[If Statement]: #if-statement +[For Statement]: #for-statement +[Builtin functions]: #builtin-functions +[Tokens]: #tokens +[Predeclared Identifiers]: #predeclared-identifiers [UTF-8]: https://en.wikipedia.org/wiki/UTF-8 -[Characters]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#characters -[Letter and Digits]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#letter-and-digits -[Comments]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#comments -[Semicolons]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#semicolons -[Identifiers]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#identifiers -[Keywords]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#keywords -[Operators and Punctuation]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#operators-and-punctuation -[Integer Literal]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#integer-literal -[Floating-point Literal]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#floating-point-literal -[Rune Literal]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#rune-literal -[String Literal]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#string-literal -[Boolean Literal]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#boolean-literal -[Blocks]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#blocks -[Declarations]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#declarations -[Operands]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#operands -[Calls]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#calls -[Operators]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#operators -[Arithmetic Operators]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#arithmetic-operators -[Conversion]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#conversion -[Blank Statement]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#blank-statement -[Assignment]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#assignment -[If Statement]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#if-statement -[For Statement]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#for-statement -[Break and Continue Statement]: https://github.com/Wopslang/Wops/blob/main/doc/grammar.md#break-and-continue-statement +[Characters]: #characters +[Letter and Digits]: #letter-and-digits +[Comments]: #comments +[Semicolons]: #semicolons +[Identifiers]: #identifiers +[Keywords]: #keywords +[Operators and Punctuation]: #operators-and-punctuation +[Integer Literal]: #integer-literal +[Floating-point Literal]: #floating-point-literal +[Rune Literal]: #rune-literal +[String Literal]: #string-literal +[Boolean Literal]: #boolean-literal +[Blocks]: #blocks +[Declarations]: #declarations +[Operands]: #operands +[Calls]: #calls +[Operators]: #operators +[Arithmetic Operators]: #arithmetic-operators +[Conversion]: #conversion +[Blank Statement]: #blank-statement +[Assignment]: #assignment +[If Statement]: #if-statement +[For Statement]: #for-statement +[Break and Continue Statement]: #break-and-continue-statement [ext-link-1]: https://github.com/Wopslang/Wops/blob/main/lib/functions.md © 2021 Wops Team From 0b6b6e302df46f15c0fa3f4155d245ed9ec7e94d Mon Sep 17 00:00:00 2001 From: RedoC-github Date: Tue, 3 Oct 2023 23:57:23 +0900 Subject: [PATCH 5/5] update grammar.md --- README.md | 11 +++--- doc/grammar.md | 103 ++++++++++++++++++++++++++++--------------------- 2 files changed, 64 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 837e536..cd61f57 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,13 @@ [![Badge](https://img.shields.io/badge/Slack-Join_our_chat-critical.svg?link=https://join.slack.com/t/wopslangcommunity/shared_invite/zt-nkcy12cy-n8YlAPnOT~ErPODF6k3jOw&logo=slack)](https://join.slack.com/t/wopslangcommunity/shared_invite/zt-nkcy12cy-n8YlAPnOT~ErPODF6k3jOw) ```go -func Wopslang(contributors) { - int newIdea = 0 - for i in contributors { - newIdea += 100 +func Wopslang = {contributors, { + int newIdea = 0 + for i in contributors $ + newIdea += 100 + ; + return bindAll(newIdea, Whops) } - return bindAll(newIdea, Whops) } ``` diff --git a/doc/grammar.md b/doc/grammar.md index 878d3cc..e5a4583 100644 --- a/doc/grammar.md +++ b/doc/grammar.md @@ -85,7 +85,7 @@ digit = "0" ... "9" . Comments can be represented with a form: -- Full-line comments start with the character sequence // and stop at the end of the line. +- Full-line comments start with `//` and stop at the end of the line. For example, this form will be allowed: @@ -130,8 +130,6 @@ Some identifiers cannot be used. See [Predeclared Identifiers] for more detail. The following keywords are reserved and may not be used as identifiers. -> We only put keywords which work on v0.1 alpha, so it can be updated. - ```text break const continue elif else for @@ -142,8 +140,6 @@ if range The following character sequences represent operators (including assignment operators) and punctuation. -> We only put operators and punctuation which work on v0.1 alpha, so it can be updated. - ```text + && == != ( ) - || < <= [ ] @@ -155,10 +151,10 @@ The following character sequences represent operators (including assignment oper An integer literal is a sequence of digits representing an integer constant. -> In version v0.1, only decimal is allowed to use. +> In version v0.x, only decimal is allowed to use. ```ebnf -integer_lit = "0" | ( "1" … "9" ) [ decimal_digits ] . +integer_lit = "0" | ("1" … "9") [ decimal_digits ] . decimal_digits = { digit } . ``` @@ -178,10 +174,10 @@ a56bc // (x) ### Boolean Literal -boolean literal is a bit representing boolean constant: *true, and false*. +boolean literal is a bit representing boolean constant: *true `1`, and false `0`*. ```ebnf -bool_lit = "0" | "1". +bool_lit = "0" | "1". ``` ### Floating-point Literal @@ -254,10 +250,10 @@ There are four kind of type: *Integer, Floating-Point, String, and Boolean*. And |Identifier|Matching Type|Description| |---|---|--| -|int|Integer|signed 32-bit integers (-2147483648 ~ 2147483647)| -|double|Floating-Point|IEEE-754 64-bit floating-point numbers| -|string|String|[same with string literal][String Literal]| -|bool|Boolean|[same with boolean literal][Boolean Literal]| +|`int`|Integer|signed 32-bit integers (-2147483648 ~ 2147483647)| +|`double`|Floating-Point|IEEE-754 64-bit floating-point numbers| +|`string`|String|[same with string literal][String Literal]| +|`bool`|Boolean|[same with boolean literal][Boolean Literal]| ```ebnf Type = "int" | "double" | "string" | "bool" . @@ -287,6 +283,16 @@ Containers can be represented like following code. {1, 2, {3, 4}, 5} ``` +Special operator (only available in container) `~` denotes the arithmetic integer sequence when it is used in a container. +Basicaly, `a~b` denotes a sequence from `a` to `b` with interval `1`. You can also decide specific interval `c` using `a~b~c`. + +```go +{0 ~ 10} // {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} +{1 ~ 10 ~ 3} // {1,4,7,10} +``` + +You should aware that `~` contains last element. + Because objects use containers to store their data, you can assign a container to an object instead of assigning an object to it. You can also use a container instantly in for statement. However, you can't declare a container directly and use it. @@ -304,10 +310,10 @@ for i in arr $ An object is a general storage for various data structures. Objects store data in their *container*. ### Object Types -Some types, called *object types*, can be used to represent what the object stores in their container. Three types of object types can be used in Wopslang v0.2: *Array*, *Func*, and *Object*. *Array* and *Func* are the predefined object types and represent a list of values and a function, respectively. You can define your objects by using *Object* object type. +Some types, called *object types*, can be used to represent what the object stores in their container. Three types of object types can be used in Wopslang v0.2: `Array`, `Func`, and `Object`. `Array` and `Func` are the predefined object types and represent a list of values and a function, respectively. You can define your objects by using `Object` object type. ```ebnf -ObjectType = ("Array" | "Func" | "Object") ObjType +ObjectType = ( "Array" | "Func" | "Object" ) ObjType ``` If the object type has argument type, you can add argument types like @@ -327,8 +333,8 @@ NOT CONFIRMED YET. STAY TUNED. ### Declaring Object There are two ways of assigning a value to an object used in Wopslang v0.2: *Assigning an object directly*, and *Assigning a container into the object's container*. -The former uses the regular operator *=* and works the same with assigning variables. -Latter uses the special operator *<-* only available in Object. +The former uses the regular operator `=` and works the same with assigning variables. +Latter uses the special operator `<-` only available in Object. As for adding const keyword, of course, you can declare a constant object with a mandatory initial value. ```go @@ -340,7 +346,7 @@ Func<{int, int} int> sum <- {int a, int b, { return a+b }} ### Blocks -A block is an empty sequence of declarations and statements within : and ;. +A block is an empty sequence of declarations and statements within `:` and `;`. ```ebnf Block = ":" [ Statements ] ";" . @@ -374,7 +380,9 @@ Operands represent the elementary values in an expression. It can be a *literal* Operand = Literal | OpndName | "(" Expression ")" | Container | ObjType. Literal = integer_lit | float_lit | rune_lit | string_lit . OpndName = identifier . -Container = "{" { (Container | Statement) "," } [Container | Statement] "}" . +Container = ConGrp | ConRan . +ConGrp = "{" { ( Container | Statement ) "," } [ Container | Statement ] "}" . +ConRan = "{" Expression "~" Expression [ "~" Expression ] "}" . ObjType = "<" {Container | Type} ">" . ``` @@ -384,7 +392,7 @@ Also, there are some unit expression groups: UnitExpr = Operand | UnitExpr Arguments . -Arguments = "(" ([Expression] | { Expression "," } Expression ) ")" . +Arguments = "(" ( [ Expression ] | { Expression "," } Expression ) ")" . ``` For example: @@ -399,8 +407,6 @@ out("Hello, World!\n", "Nice to meet you :D") ### Calls -> Defining function will be supported with Func object. - Here is the basic formation of function: ```ebnf @@ -441,19 +447,20 @@ Each operator has a different priority for parsing. For instance, unary operator |2|`&&`| |1|`\|\|`| -The leftmost operator in the same priority has a higher priority. For instance, a+b-c is the same with (a+b)-c. +The leftmost operator in the same priority has a higher priority. +For instance, `a+b-c` is the same with `(a+b)-c`. ### Arithmetic Operators |Operator|Matching Literals| |--------|-----------------| -|+|integer, float, string| -|-|integer, float| -|*|integer, float| -|/|integer, float| -|%|integer| +|`+`|integer, float, string| +|`-`|integer, float| +|`*`|integer, float| +|`/`|integer, float| +|`%`|integer| -If you divide by zero(A/0 or A%0, A:expression), interpreter will emit the error. Also, ***If you divide with integers, the result will be integer, too.*** +If you divide by zero(`A/0` or `A%0`, A:expression), interpreter will emit the error. Also, ***If you divide with integers, the result will be integer, too.*** ### Conversion @@ -501,7 +508,7 @@ d <- {1 ~ 5} // d = {1, 2, 3, 4, 5} ### If Statement -*If* statements specify the conditional execution of more than two branches according to the value of a boolean expression. If value is true, "if" branches will be executed, otherwise, highest "elif" branches will be executed. If every "if" and "elif" branches' expression is false, "else" branch will be executed. +*If* statements specify the conditional execution of more than two branches according to the value of a boolean expression. If value is true, `if` branches will be executed, otherwise, highest `elif` branches will be executed. If every `if` and `elif` branches' expression is false, `else` branch will be executed. For example: @@ -522,22 +529,21 @@ If a is 1, `A`'ll be executed. If a is larger than 1, `B`'ll be executed. For `C ```ebnf IfStmt = "if" Expression IfBlock { ";" Expression IfBlock } - [";" IfBlock] + [ ";" IfBlock ] ``` ### For Statement -*For* statement represent repeating execution of a block. There are three forms: *a single condition, a "for" clause*. - -> Note: there isn't a "range" clause because there isn't any array system in Wopslang v0.1. +*For* statement represents repeating execution of a block. +There are two types of for statement: *using a single condition*, and *using an array*. ```ebnf -ForStmt = "for" ( Expression | ForClause ) ForBlock . +ForStmt = "for" ( Expression | ArrayClause ) ForBlock . ``` #### For statement with single condition -A *for* statement with single condition represent repeating execution of a block as long as a boolean condition evaluates to true. The condition is evaluated before each iteration. +A *for* statement with single condition represents repeating execution of a block as long as a boolean condition evaluates to true. The condition is evaluated before each iteration. ```go for a % 2 $ @@ -545,26 +551,30 @@ for a % 2 $ ; ``` -#### For statement with for clause +#### For statement with an array -A *for* statement with for clause is based on `range` function. There are three arguments of `range` function: *start, end, and step*. Then, `range` function create an array which is [start, end) when step is one(it's almost same with *python's range function*). Finally, *for* statement repeat execution of a block with that array. **Remember that range function doesn't include end index's element** +A *for* statement with an array assigns one element from the array to a local variable in order at every iteration. ```ebnf -ForClause = identifiers "in" Expression "~" Expression "~" Expression" . +ArrayClause = identifiers "in" Expression. ``` ```go -for i in 0~6~2 $ - out(tostring(i) + "\n") +Array arr <- {0 ~ 4 ~ 2} +for i in arr $ + for j in {"a", "b", "c"} $ + out(tostring(i) + j + " ") + ; + out("\n") ; ``` Output: ```text -0 -2 -4 +0a 0b 0c +2a 2b 2c +4a 4b 4c ``` ### Break and Continue Statement @@ -591,6 +601,9 @@ Redirect to [here][ext-link-1] [Variables]: #variables [Containers]: #containers [Objects]: #objects +[Object Types]: #object-types +[Defining Object]: #defining-object +[Declaring Object]: #declaring-object [Expressions]: #expressions [Statements]: #statements [If Statement]: #if-statement @@ -625,4 +638,4 @@ Redirect to [here][ext-link-1] [Break and Continue Statement]: #break-and-continue-statement [ext-link-1]: https://github.com/Wopslang/Wops/blob/main/lib/functions.md -© 2021 Wops Team +© 2023 Wops Team