-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for ChaiScript (#2706)
- Loading branch information
1 parent
04ef309
commit 3f7d745
Showing
15 changed files
with
571 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
Prism.languages.chaiscript = Prism.languages.extend('clike', { | ||
'string': { | ||
pattern: /(^|[^\\])'(?:[^'\\]|\\[\s\S])*'/, | ||
lookbehind: true, | ||
greedy: true | ||
}, | ||
'class-name': [ | ||
{ | ||
// e.g. class Rectangle { ... } | ||
pattern: /(\bclass\s+)\w+/, | ||
lookbehind: true | ||
}, | ||
{ | ||
// e.g. attr Rectangle::height, def Rectangle::area() { ... } | ||
pattern: /(\b(?:attr|def)\s+)\w+(?=\s*::)/, | ||
lookbehind: true | ||
} | ||
], | ||
'keyword': /\b(?:attr|auto|break|case|catch|class|continue|def|default|else|finally|for|fun|global|if|return|switch|this|try|var|while)\b/, | ||
'number': [ | ||
Prism.languages.cpp.number, | ||
/\b(?:Infinity|NaN)\b/ | ||
], | ||
'operator': />>=?|<<=?|\|\||&&|:[:=]?|--|\+\+|[=!<>+\-*/%|&^]=?|[?~]|`[^`\r\n]{1,4}`/, | ||
}); | ||
|
||
Prism.languages.insertBefore('chaiscript', 'operator', { | ||
'parameter-type': { | ||
// e.g. def foo(int x, Vector y) {...} | ||
pattern: /([,(]\s*)\w+(?=\s+\w)/, | ||
lookbehind: true, | ||
alias: 'class-name' | ||
}, | ||
}); | ||
|
||
Prism.languages.insertBefore('chaiscript', 'string', { | ||
'string-interpolation': { | ||
pattern: /(^|[^\\])"(?:[^"$\\]|\\[\s\S]|\$(?!\{)|\$\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\})*"/, | ||
lookbehind: true, | ||
greedy: true, | ||
inside: { | ||
'interpolation': { | ||
pattern: /((?:^|[^\\])(?:\\{2})*)\$\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\}/, | ||
lookbehind: true, | ||
inside: { | ||
'interpolation-expression': { | ||
pattern: /(^\$\{)[\s\S]+(?=\}$)/, | ||
lookbehind: true, | ||
inside: Prism.languages.chaiscript | ||
}, | ||
'interpolation-punctuation': { | ||
pattern: /^\$\{|\}$/, | ||
alias: 'punctuation' | ||
} | ||
} | ||
}, | ||
'string': /[\s\S]+/ | ||
} | ||
}, | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<h2>Full example</h2> | ||
<pre><code>// http://chaiscript.com/examples.html#ChaiScript_Language_Examples | ||
// Source: https://gist.github.com/lefticus/cf058f2927fef68d58e0#file-chaiscript_overview-chai | ||
|
||
// ChaiScript supports the normal kind of control blocks you've come to expect from | ||
// C++ and JavaScript | ||
|
||
|
||
if (5 > 2) { | ||
print("Yup, 5 > 2"); | ||
} else if (2 > 5) { | ||
// never gonna happen | ||
} else { | ||
// really not going to happen | ||
} | ||
|
||
var x = true; | ||
|
||
while (x) | ||
{ | ||
print("x was true") | ||
x = false; | ||
} | ||
|
||
for (var i = 1; i < 10; ++i) | ||
{ | ||
print(i); // prints 1 through 9 | ||
} | ||
|
||
|
||
// function definition | ||
|
||
def myFunc(x) { | ||
print(x); | ||
} | ||
|
||
// function definition with function guards | ||
def myFunc(x) : x > 2 && x < 5 { | ||
print(to_string(x) + " is between 2 and 5") | ||
} | ||
|
||
def myFunc(x) : x >= 5 { | ||
print(t_string(x) + " is greater than or equal to 5") | ||
} | ||
|
||
myFunc(3) | ||
|
||
// ChaiScript also supports in string evaluation, which C++ does not | ||
|
||
print("${3 + 5} is 8"); | ||
|
||
// And dynamic code evaluation | ||
|
||
var value = eval("4 + 2 + 12"); | ||
|
||
// value is equal to 18</code></pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
true | ||
false | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["boolean", "true"], | ||
["boolean", "false"] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// comment | ||
/* | ||
comment | ||
*/ | ||
|
||
/* | ||
comment | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["comment", "// comment"], | ||
["comment", "/*\r\n comment\r\n*/"], | ||
|
||
["comment", "/*\r\n comment"] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
attr Rectangle::height | ||
attr Rectangle::width | ||
def Rectangle::Rectangle() { this.height = 10; this.width = 20 } | ||
def Rectangle::area() { this.height * this.width } | ||
var rect = Rectangle() | ||
rect.height = 30 | ||
print(rect.area()) | ||
|
||
class Rectangle { | ||
attr height | ||
attr width | ||
def Rectangle() { this.height = 10; this.width = 20 } | ||
def area() { this.height * this.width } | ||
} | ||
var rect = Rectangle() | ||
rect.height = 30 | ||
print(rect.area()) | ||
|
||
def update(dt) { | ||
x = x + 20.0f * dt | ||
} | ||
|
||
def foo(int x, Vector y) {} | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["keyword", "attr"], | ||
["class-name", "Rectangle"], | ||
["operator", "::"], | ||
"height\r\n", | ||
|
||
["keyword", "attr"], | ||
["class-name", "Rectangle"], | ||
["operator", "::"], | ||
"width\r\n", | ||
|
||
["keyword", "def"], | ||
["class-name", "Rectangle"], | ||
["operator", "::"], | ||
["function", "Rectangle"], | ||
["punctuation", "("], | ||
["punctuation", ")"], | ||
["punctuation", "{"], | ||
["keyword", "this"], | ||
["punctuation", "."], | ||
"height ", | ||
["operator", "="], | ||
["number", "10"], | ||
["punctuation", ";"], | ||
["keyword", "this"], | ||
["punctuation", "."], | ||
"width ", | ||
["operator", "="], | ||
["number", "20"], | ||
["punctuation", "}"], | ||
|
||
["keyword", "def"], | ||
["class-name", "Rectangle"], | ||
["operator", "::"], | ||
["function", "area"], | ||
["punctuation", "("], | ||
["punctuation", ")"], | ||
["punctuation", "{"], | ||
["keyword", "this"], | ||
["punctuation", "."], | ||
"height ", | ||
["operator", "*"], | ||
["keyword", "this"], | ||
["punctuation", "."], | ||
"width ", | ||
["punctuation", "}"], | ||
|
||
["keyword", "var"], | ||
" rect ", | ||
["operator", "="], | ||
["function", "Rectangle"], | ||
["punctuation", "("], | ||
["punctuation", ")"], | ||
|
||
"\r\nrect", | ||
["punctuation", "."], | ||
"height ", | ||
["operator", "="], | ||
["number", "30"], | ||
|
||
["function", "print"], | ||
["punctuation", "("], | ||
"rect", | ||
["punctuation", "."], | ||
["function", "area"], | ||
["punctuation", "("], | ||
["punctuation", ")"], | ||
["punctuation", ")"], | ||
|
||
["keyword", "class"], | ||
["class-name", "Rectangle"], | ||
["punctuation", "{"], | ||
|
||
["keyword", "attr"], | ||
" height\r\n ", | ||
|
||
["keyword", "attr"], | ||
" width\r\n ", | ||
|
||
["keyword", "def"], | ||
["function", "Rectangle"], | ||
["punctuation", "("], | ||
["punctuation", ")"], | ||
["punctuation", "{"], | ||
["keyword", "this"], | ||
["punctuation", "."], | ||
"height ", | ||
["operator", "="], | ||
["number", "10"], | ||
["punctuation", ";"], | ||
["keyword", "this"], | ||
["punctuation", "."], | ||
"width ", | ||
["operator", "="], | ||
["number", "20"], | ||
["punctuation", "}"], | ||
|
||
["keyword", "def"], | ||
["function", "area"], | ||
["punctuation", "("], | ||
["punctuation", ")"], | ||
["punctuation", "{"], | ||
["keyword", "this"], | ||
["punctuation", "."], | ||
"height ", | ||
["operator", "*"], | ||
["keyword", "this"], | ||
["punctuation", "."], | ||
"width ", | ||
["punctuation", "}"], | ||
|
||
["punctuation", "}"], | ||
|
||
["keyword", "var"], | ||
" rect ", | ||
["operator", "="], | ||
["function", "Rectangle"], | ||
["punctuation", "("], | ||
["punctuation", ")"], | ||
|
||
"\r\nrect", | ||
["punctuation", "."], | ||
"height ", | ||
["operator", "="], | ||
["number", "30"], | ||
|
||
["function", "print"], | ||
["punctuation", "("], | ||
"rect", | ||
["punctuation", "."], | ||
["function", "area"], | ||
["punctuation", "("], | ||
["punctuation", ")"], | ||
["punctuation", ")"], | ||
|
||
["keyword", "def"], | ||
["function", "update"], | ||
["punctuation", "("], | ||
"dt", | ||
["punctuation", ")"], | ||
["punctuation", "{"], | ||
|
||
"\r\n x ", | ||
["operator", "="], | ||
" x ", | ||
["operator", "+"], | ||
["number", "20.0f"], | ||
["operator", "*"], | ||
" dt\r\n", | ||
|
||
["punctuation", "}"], | ||
|
||
["keyword", "def"], | ||
["function", "foo"], | ||
["punctuation", "("], | ||
["parameter-type", "int"], | ||
" x", | ||
["punctuation", ","], | ||
["parameter-type", "Vector"], | ||
" y", | ||
["punctuation", ")"], | ||
["punctuation", "{"], | ||
["punctuation", "}"] | ||
] |
Oops, something went wrong.