diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 00000000..3c9a1c23 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1 @@ +pocketpy.h diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..be5db77c --- /dev/null +++ b/examples/README.md @@ -0,0 +1,12 @@ +# TODO +- variations of print function +- variable declaration +- string, list, tuple, dictionary +- basic operations +- if else +- for loop, while loop +- json loads, dumps +- exception handling + + +clang++ --std=c++17 cjson.cpp -o cjson diff --git a/examples/cjson.cpp b/examples/cjson.cpp new file mode 100644 index 00000000..76ce7aa2 --- /dev/null +++ b/examples/cjson.cpp @@ -0,0 +1,19 @@ +#include "pocketpy.h" + +using namespace pkpy; + +int main(){ + // Create a virtual machine + VM* vm = new VM(); + + // cjson loads and dumps! + vm->exec("import cjson"); + vm->exec("dict = {'a': 1, 'b': [1, 3, 'Hello World'], 'c': {'a': 4}, 'd': None, 'd': True }"); + vm->exec("json_str = cjson.dumps(dict)"); + vm->exec("print(json_str)"); + vm->exec("loaded_dict = cjson.loads(json_str)"); + vm->exec("print(loaded_dict)"); + + delete vm; + return 0; +} diff --git a/examples/json.cpp b/examples/json.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/string_formatting.cpp b/examples/string_formatting.cpp index e69de29b..8a25d3d9 100644 --- a/examples/string_formatting.cpp +++ b/examples/string_formatting.cpp @@ -0,0 +1,57 @@ +#include +#include "pocketpy.h" + +using namespace pkpy; + + +int main(){ + // Create a virtual machine + VM* vm = new VM(); + + //prints: Hello, World! + vm->exec("print(\"Hello, {}!\".format(\"World\"))"); + + //prints: I love Python + vm->exec("print('{} {} {}'.format('I', 'love', 'Python'))"); + + //prints: I love Python + vm->exec("print('{0} {1} {2}'.format('I', 'love', 'Python'))"); + + //prints: Python love I + vm->exec("print('{2} {1} {0}'.format('I', 'love', 'Python'))"); + + //prints: pythonlovepython + vm->exec("print('{0}{1}{0}'.format('python', 'love'))"); + + //prints + vm->exec("print('{k}={v}'.format(k='key', v='value'))"); + + vm->exec("print('{k}={k}'.format(k='key'))"); + + vm->exec("print('{0}={1}'.format('{0}', '{1}'))"); + + vm->exec("print('{{{0}}}'.format(1))"); + + vm->exec("print('{0}{1}{1}'.format(1, 2, 3))"); + + vm->exec("try:"); + vm->exec("@indent print('{0}={1}}'.format(1, 2)')"); + vm->exec(" exit(1)"); + vm->exec("except ValueError:"); + vm->exec(" print('ValueError')"); + vm->exec("print('{{{}xxx{}x}}'.format(1, 2))"); + vm->exec("print('{{abc}}'.format())"); + +// assert "{{{}xxx{}x}}".format(1, 2) == "{1xxx2x}" +// assert "{{abc}}".format() == "{abc}" + // cjson loads and dumps! + // vm->exec("import cjson"); + // vm->exec("dict = {'a': 1, 'b': [1, 3, 'Hello World'], 'c': {'a': 4}, 'd': None, 'd': True }"); + // vm->exec("json_str = cjson.dumps(dict)"); + // vm->exec("print(json_str)"); + // vm->exec("loaded_dict = cjson.loads(json_str)"); + // vm->exec("print(loaded_dict)"); + + delete vm; + return 0; +}