Skip to content

Instantly share code, notes, and snippets.

@Journeyman1337
Last active December 5, 2021 03:21
Show Gist options
  • Save Journeyman1337/88cd5a6d17cf53fd17c00557f7724bd6 to your computer and use it in GitHub Desktop.
Save Journeyman1337/88cd5a6d17cf53fd17c00557f7724bd6 to your computer and use it in GitHub Desktop.
BracketScript context free grammar and prototype
Copyright (c) 2021 Daniel Valcour
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Spaces are not between elements by default. | means or. + after an element means that it repeats one or more times, but must be
at least once. ? after an element means it may repeat multiple times or not be present at all. _ means nothing. * means any
character. Two characters seperated by .. means those two characters and all between. Comments are not included.
<ws> -> ( " " | "\n" | "\t" )?;
<unsigned_number> -> ( 0..9 )+;
<unsigned_decimal> -> <unsigned_number>+ "." <unsigned_number>+;
<string> -> "\"" *+ "\"";
<negation_operator> -> "!";
<resolution_operator> -> ":";
<byte_type> -> "b";
<sbyte_type> -> "sb";
<ushort_type> -> "us";
<short_type> -> "s";
<uint_type> -> "ui";
<int_type> -> "i";
<ulong_type> -> "ul";
<long_type> -> "l";
<float_type> -> "f";
<double_type> -> "d";
<string_type> -> "str";
<signed_number> -> "-"? <unsigned_number>;
<signed_decimal> -> "-"? <unsigned_decimal>;
<byte_literal> -> ( ( <byte_type> <resolution_operator> <ws>? <unsigned_number> ) | "true" | "false" | ( "'" * "'" ) );
<sbyte_literal> -> <sbyte_type> <resolution_operator> <ws>? <signed_number>;
<ushort_literal> -> <ushort_type> <resolution_operator> <ws>? <unsigned_number>;
<short_literal> -> <short_type> <resolution_operator> <ws>? <signed_number>;
<uint_literal> -> <uint_type> <resolution_operator> <ws>? <unsigned_number>;
<int_literal> -> ( ( <int_type> <resolution_operator> <ws>? ) | _ ) <signed_number>;
<ulong_literal> -> <ulong_type> <resolution_operator> <ws>? <unsigned_number>;
<long_literal> -> <long_type> <resolution_operator> <ws>? <number>;
<float_literal> -> ( ( <float_type> <resolution_operator> <ws>? ) | _ ) ( <signed_decimal> | <signed_number> );
<double_literal> -> <double_type> <resolution_operator> <ws>? ( <signed_decimal> | <signed_number> );
<string_literal> -> ( ( <string_type> <resolution_operator> <ws>? | _ ) "\"" *+ "\"" );
<numeric_literal> -> ( <negation_operator> | _ )? ( <byte_literal> | <sbyte_literal> | <ushort_literal> | <short_literal> |
<uint_literal> | <int_literal> | <ulong_literal> | <float_literal> | <double_literal> );
<literal> -> ( <numeric_literal> | <string_literal> );
<name> -> ( "_" | a..z | A..Z ) ( "_" | a..z | A..Z | 0..9 )?;
<identifier> -> <name> ( <resolution_operator> <ws>? <name> )? ( <resolution_operator> <ws>? <unsigned_number> )?;
<assignment_operator> -> ( "=" | "-=" | "+=" | "*=" | "/=" | "%=" | "^=" | "$=" );
<arithmatic_operator> -> ( "+" | "-" | "*" | "/" | "^" | "$" );
<comparison_operator> -> ( ">" | "<" | ">=" | "<=" | "==" | "!=" | "||" | "&&" );
<bit_operator> -> ( "|" | "&" | "~" );
<equatable_operator> -> ( <arithmatic_operator> | <comparison_operator> | <bit_operator> )
<equation> -> "(" <ws>? <value> <ws>? ( <arithmatic_operator> <ws>? <value> <ws>? )+ ")";
<assigning_equation> -> "(" <ws>? ( <identifier> <ws>? <assignment_operator> <ws>? )? <value> <ws>? ( <arithmatic_operator>
<ws>? <value> <ws>? )+ ")";
<operation> -> "[" <ws>? <identifier> ( <ws>+ <value> )? <ws>? "]";
<list> -> ( <name> <resolution_operator> <ws>? )? "{" <ws>? <value> ( <ws>+ <value> )? <ws>? "}";
<value> -> ( <literal> | <equation> | <operation> | <identifier> | <list> );
<script> -> ( <ws>? ( <operation> | <assigning_equation> ) )? <ws>?;
/*
Copyright (c) 2021 Daniel Valcour
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
[= myvar 5]
[+= myvar 7]
[*= myvar [% 5 3]]
[/= myvar (3 * 2 - 6 % 3)]
[%= myvar ((3 ^ 2) + 3)]
(myvar ^= 1 + 1)
[= mydouble d:53.22]
(mydouble += d:533.2 / d:2445.5)
[= mystring "Hello,"]
[+= mystring " world!"]
[print mystring]
[if (mydouble > d:50.0)
[print "it is greater than 50"]
]
[elif [> mydouble d:20.0]
[print "it greater than 20"]
]
[else
[print "none pass"]
]
[def factorial n
[if [<= n 1]
[return 1]
]
[else
[return [* n [factorial [- n 1]]]]
]
]
[for (x = 0) (x < 100) (x += 1)
[print x " " [factorial x]]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment