GeezerVison is showing github.com — Add: import · CrazyProger1/FastPy@3bafe67
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{ | | 2 | + "builtin_functions": { | | 3 | + "import": "import", | | 4 | + "log": "log" | | 5 | + }, | | 6 | + "builtin_types": { | | 7 | + "string": "str", | | 8 | + "integer": "int", | | 9 | + "boolean": "bool" | | 10 | + } | | 11 | +} |
Collapse file Copy file name to clipboardExpand all lines: config/transpiler.json +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | "transpiler_class": "fastpy.transpiler.transpilers.Transpiler", | 3 | 3 | "cpp_main_template": "minimal_main.cpp", | 4 | 4 | "cpp_templates_dir": "cpp_code", | | 5 | + "cpp_template": "minimal.cpp", | | 6 | + | 5 | 7 | "node_transpiling": { | 6 | 8 | "fastpy.parser.nodes.AssignNode": "fastpy.transpiler.node_transpilers.AssignNodeTranspiler", | 7 | 9 | "fastpy.parser.nodes.ValueNode": "fastpy.transpiler.node_transpilers.ValueNodeTranspiler", | |
Collapse file Copy file name to clipboard +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#pragma once | | 2 | + | | 3 | +{% block includes %} | | 4 | + | | 5 | +{% endblock %} | | 6 | + | | 7 | + | | 8 | +{{ additional_includes }} | | 9 | + | | 10 | + | | 11 | +{% block builtin_types %} | | 12 | + | | 13 | +{% endblock %} | | 14 | + | | 15 | + | | 16 | +{% block builtin_functions %} | | 17 | + | | 18 | +{% endblock %} | | 19 | + | | 20 | + | | 21 | +{{ external_code }} | | 22 | + | | 23 | + | | 24 | +{{ internal_code }} |
Collapse file Copy file name to clipboard +46Lines changed: 46 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{% extends 'base.cpp' %} | | 2 | + | | 3 | +{% block includes %} | | 4 | +#include<iostream> | | 5 | +#include<string> | | 6 | +#include"termcolor.hpp" | | 7 | +{% endblock %} | | 8 | + | | 9 | + | | 10 | +{% block builtin_types %} | | 11 | + | | 12 | +typedef std::string str; | | 13 | + | | 14 | +{% endblock %} | | 15 | + | | 16 | + | | 17 | +{% block builtin_functions %} | | 18 | +template<typename T> | | 19 | +void log(const T &t, bool endl = false) { | | 20 | + std::cout << t; | | 21 | + if (endl) { | | 22 | + std::cout << std::endl; | | 23 | + } | | 24 | + std::cout << termcolor::reset; | | 25 | +} | | 26 | + | | 27 | + | | 28 | +template<typename T> | | 29 | +void log_error(const T &t, bool endl = false) { | | 30 | + std::cout << termcolor::on_red << termcolor::grey; | | 31 | + log(t, endl); | | 32 | +} | | 33 | + | | 34 | +template<typename T> | | 35 | +void log_info(const T &t, bool endl = false) { | | 36 | + std::cout << termcolor::on_blue << termcolor::grey; | | 37 | + log(t, endl); | | 38 | + | | 39 | +} | | 40 | + | | 41 | +template<typename T> | | 42 | +void log_warning(const T &t, bool endl = false) { | | 43 | + std::cout << termcolor::on_yellow << termcolor::grey; | | 44 | + log(t, endl); | | 45 | +} | | 46 | +{% endblock %} |
Collapse file Copy file name to clipboard +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +from ..config import JsonConfig, CONFIG_FOLDER | | 2 | +from ..filesystem import FileSystem as Fs | | 3 | + | | 4 | +builtin_config = JsonConfig( | | 5 | + filepath=Fs.join(CONFIG_FOLDER, 'builtin.json'), | | 6 | + authoload=True | | 7 | +) | | 8 | + | | 9 | +BUILTIN_TYPES = builtin_config['builtin_types'] | | 10 | +BUILTIN_FUNCTIONS = builtin_config['builtin_functions'] |
Collapse file Copy file name to clipboardExpand all lines: fastpy/dev_kit/transpiler.py +8-4Lines changed: 8 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | from fastpy.filesystem import FileSystem as Fs | 2 | 2 | from fastpy.log import Logger | 3 | 3 | from fastpy.lexer import create_lexer, BaseToken | 4 | | -from fastpy.parser import create_parser, BaseAST, ImportNode | | 4 | +from fastpy.parser import create_parser, BaseAST | | 5 | +from fastpy.parser.nodes import CallNode | 5 | 6 | from fastpy.module import Module | 6 | 7 | from fastpy.transpiler import create_transpiler | | 8 | +from .config import BUILTIN_FUNCTIONS | 7 | 9 |
| 8 | 10 |
| 9 | 11 | class TranspileAPI: | @@ -63,9 +65,11 @@ def _transpile_file(self, module: Module) -> str: | 63 | 65 |
| 64 | 66 | # importing modules are processed after parsing | 65 | 67 | for node in ast.nodes(module.name): | 66 | | - if isinstance(node, ImportNode): | 67 | | - importing_file = Fs.normalize_path(node.filepath.text) | 68 | | - self._transpile_file(Module(importing_file)) | | 68 | + if isinstance(node, CallNode): | | 69 | + if node.identifier.text == BUILTIN_FUNCTIONS['import']: | | 70 | + for importing_file in node.arguments: | | 71 | + importing_file = Fs.normalize_path(importing_file.value.text) | | 72 | + self._transpile_file(Module(importing_file)) | 69 | 73 |
| 70 | 74 | # third step: transpiling | 71 | 75 | cpp_code = self._translate_file(module, ast) | |
Collapse file Copy file name to clipboardExpand all lines: fastpy/parser/nodes.py +10-10Lines changed: 10 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -191,13 +191,13 @@ def line(self) -> int: | 191 | 191 | return self.condition[0].line | 192 | 192 |
| 193 | 193 |
| 194 | | -class ImportNode(BasicNode, PrintableNode): | 195 | | - def __init__(self, filepath: BaseToken = None, parts: list[BaseToken] = None): | 196 | | - self.filepath = filepath | 197 | | - self.parts = parts | 198 | | - | 199 | | - @property | 200 | | - def line(self) -> int: | 201 | | - if not self.filepath: | 202 | | - return -1 | 203 | | - return self.filepath.line | | 194 | +# class ImportNode(BasicNode, PrintableNode): | | 195 | +# def __init__(self, filepath: BaseToken = None, parts: list[BaseToken] = None): | | 196 | +# self.filepath = filepath | | 197 | +# self.parts = parts | | 198 | +# | | 199 | +# @property | | 200 | +# def line(self) -> int: | | 201 | +# if not self.filepath: | | 202 | +# return -1 | | 203 | +# return self.filepath.line |
Collapse file Copy file name to clipboardExpand all lines: fastpy/transpiler/config.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 |
| 11 | 11 | CPP_TEMPLATES_DIR = transpiler_config['cpp_templates_dir'] | 12 | 12 | CPP_MAIN_TEMPLATE_PATH = transpiler_config['cpp_main_template'] | | 13 | +CPP_TEMPLATE_PATH = transpiler_config['cpp_template'] | 13 | 14 |
| 14 | 15 | NODE_TRANSPILING = transpiler_config['node_transpiling'] | 15 | 16 |
|
Collapse file Copy file name to clipboardExpand all lines: fastpy/transpiler/transpilers.py +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,6 +46,8 @@ def _load_template(self): | 46 | 46 |
| 47 | 47 | if self._current_module.name == '__main__': | 48 | 48 | self._code_template = env.get_template(CPP_MAIN_TEMPLATE_PATH) | | 49 | + else: | | 50 | + self._code_template = env.get_template(CPP_TEMPLATE_PATH) | 49 | 51 |
| 50 | 52 | def _transpile_node(self, node: BaseNode, **kwargs) -> BaseCode: | 51 | 53 | Logger.log_info(f'Transpiling: {node.line}: {node}') | |
Collapse file Copy file name to clipboardExpand all lines: fastpy_build/src/main.cpp +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -58,7 +58,9 @@ log((1 + 1) * 1); | 58 | 58 |
| 59 | 59 |
| 60 | 60 | int main(){ | 61 | | - int a = 200; | | 61 | + import("print.fpy"); | | 62 | + | | 63 | +int a = 200; | 62 | 64 |
| 63 | 65 |
| 64 | 66 | auto b = 10; | |
|