Skip to content

Commit

Permalink
support nim statement (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
srz-zumix authored Aug 1, 2022
1 parent 8fb9c59 commit 0bca332
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 24 deletions.
5 changes: 4 additions & 1 deletion samples/command/src/nim/sample.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ from test5 import Test5
import test6 except Hoge6, Fuga6
from test7 import Test7, Fuga7
import "subdir/test8"
from test9_1 import Test9_1; import test9_2;

This comment has been minimized.

Copy link
@mdsaroj

mdsaroj Mar 2, 2023

form



{.push header:"c/testA.c".} # push test
Expand All @@ -16,7 +17,7 @@ proc c_test2*()

{.pop.}

proc c_test3*(){.header:"c/testB.c"}
proc c_test3*(){.header:"c/testB.c"};

echo "Hello, Wandbox!"
Test1()
Expand All @@ -29,6 +30,8 @@ Test6()
Test7()
Fuga7()
Test8()
Test9_1()
Test9_2()

c_test1()
c_test2()
Expand Down
2 changes: 2 additions & 0 deletions samples/command/src/nim/test9_1.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
proc Test9_1*() =
echo "Test9_1"
2 changes: 2 additions & 0 deletions samples/command/src/nim/test9_2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
proc Test9_2*() =
echo "Test9_2"
11 changes: 6 additions & 5 deletions tests/test_wandbox_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,14 @@ def test_split_statements(self):
statements = split_statements(". test.sh; source \"test data.sh\"")
self.assertEqual(2, len(statements))
self.assertEqual(". test.sh;", statements[0])
self.assertEqual("source \'test data.sh\'", statements[1])
self.assertEqual("source \"test data.sh\"", statements[1])

def test_split_statements_2(self):
statements = split_statements("include(\"test1.jl\"); include(\"test2.jl\");")
self.assertEqual(2, len(statements))
self.assertEqual("include ( test1.jl );", statements[0])
self.assertEqual("include ( test2.jl );", statements[1])
statements = split_statements("include(\"test1.jl\");; include(\"test2.jl\");")
self.assertEqual(3, len(statements))
self.assertEqual("include(\"test1.jl\");", statements[0])
self.assertEqual(";", statements[1])
self.assertEqual("include(\"test2.jl\");", statements[2])


if __name__ == '__main__':
Expand Down
30 changes: 16 additions & 14 deletions wandbox/__nim__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
from .cli import CLI
from .runner import Runner
from .__cxx__ import CxxRunner
from .utils import split_statements


class NimRunner(Runner):

IMPORT_REGEX = re.compile(r'^\s*import\s*(.*?)(\s*except\s*.*|)$')
FROM_IMPORT_REGEX = re.compile(r'^\s*from\s*(\S*?)\s*import\s*(.*?)$')
C_PROC_REGEX = re.compile(r'^\s*proc.*{.*\.header\s*:\s*([\'"].*[\'"]).*}\s*$')
PUSH_HEADER_REGEX = re.compile(r'^\s*{\.push\s*.*header\s*:\s*([\'"].*[\'"]).*}$')
IMPORT_REGEX = re.compile(r'^\s*import\s*(.*?)(\s*except\s*.*|)(;|)$')
FROM_IMPORT_REGEX = re.compile(r'^\s*from\s*(\S*?)\s*import\s*(.*?)(;|)$')
C_PROC_REGEX = re.compile(r'^\s*proc.*{.*\.header\s*:\s*([\'"].*[\'"]).*}(;|)\s*$')
PUSH_HEADER_REGEX = re.compile(r'^\s*{\.push\s*.*header\s*:\s*([\'"].*[\'"]).*}(;|)\s*$')

def __init__(self, lang, compiler, save, encoding, retry, retry_wait, prefix_chars='-'):
super(NimRunner, self).__init__(lang, compiler, save, encoding, retry, retry_wait, prefix_chars)
Expand All @@ -25,22 +26,23 @@ def make_code(self, file, filepath, filename):
files = dict()
code = ''
for line in file:
codeline = re.sub(r'\s*#.*$', '', line)
m = self.IMPORT_REGEX.match(codeline)
if m:
files.update(self.get_imports(m, filepath))
else:
m = self.FROM_IMPORT_REGEX.match(codeline)
statements = split_statements(line, commenters="#")
for statement in statements:
m = self.IMPORT_REGEX.match(statement)
if m:
files.update(self.get_from_imports(m, filepath))
files.update(self.get_imports(m, filepath))
else:
m = self.C_PROC_REGEX.match(codeline)
m = self.FROM_IMPORT_REGEX.match(statement)
if m:
files.update(self.get_c_header(m, filepath))
files.update(self.get_from_imports(m, filepath))
else:
m = self.PUSH_HEADER_REGEX.match(codeline)
m = self.C_PROC_REGEX.match(statement)
if m:
files.update(self.get_c_header(m, filepath))
else:
m = self.PUSH_HEADER_REGEX.match(statement)
if m:
files.update(self.get_c_header(m, filepath))
code += line
files[filename] = code
return files
Expand Down
9 changes: 5 additions & 4 deletions wandbox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def text_transform(value):


def statements_quote(s):
if s in ['(', ')']:
if s in ['(', ')', '{', '}']:
return s
else:
return shlex.quote(s)
Expand All @@ -40,17 +40,18 @@ def statements_join(split_command):


def split_statements(line, end_of_statement=";", commenters="#"):
sl = shlex.shlex(line, posix=True, punctuation_chars=end_of_statement)
sl = shlex.shlex(line, posix=False)
sl.commenters = commenters
sl.source = None
sl.wordchars += "!#$%&()*+,-./:;<=>?@[]^_{|}~".replace(end_of_statement, '')
statements = []
statement = []
for s in sl:
if s == end_of_statement:
statements.append(statements_join(statement) + s)
statements.append(" ".join(statement) + s)
statement = []
else:
statement.append(s)
if len(statement) > 0:
statements.append(statements_join(statement))
statements.append(" ".join(statement))
return statements

0 comments on commit 0bca332

Please sign in to comment.