Skip to content

Commit

Permalink
switch to version 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alploskov committed Jul 14, 2022
1 parent 159b3a6 commit 932515f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 18 deletions.
2 changes: 2 additions & 0 deletions kithon/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def unpack(self, _vars, value):
_type = None
if value.type == 'str':
_type = 'str'
elif isinstance(value.type, str):
_type = 'any'
elif isinstance(value.type, types['list']):
_type = value.type.el_type
elif isinstance(value.type, types['dict']):
Expand Down
1 change: 1 addition & 0 deletions kithon/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ def name(self, tree: _ast.Name):
f'{".".join(ns[:-(ln + 1)])}.{tree.id}', {}
)
if var_info: break
macro = {}
if tree.id in self.templates:
var_info |= {'own': f'macro.{tree.id}'}
macro = self.templates.get(tree.id, {})
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "kithon"
readme = "README.md"
version = "0.2.1"
version = "0.3.0"
description = "transpiler python into other language"
authors = ["Aleksey Ploskov"]
license = "MIT"
Expand Down Expand Up @@ -36,7 +36,7 @@ ptpython = {version = "^3.0.20", optional = true}

watchdog = {version = "^2.1.7", optional = true}

packed = {path = "deps/packed-0.2.0-py3-none-any.whl", optional = true}
packed = {path = "deps/packed-0.2.1-py3-none-any.whl", optional = true}

[tool.poetry.scripts]
kithon = "commands:kithon"
Expand Down
5 changes: 3 additions & 2 deletions translators/js/core.tp
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ new_var: |-
{%- if isinstance(value.type, types['module']) -%}
import * as {{var}} from {{value.parts.args[0]}};
{%- else -%}
{%if env.mod_name != 'main' and nl == 0%}export {%endif%}let {{var}} = {{value}};"
{%if env.mod_name != 'main' and nl == 0%}export {%endif%}let {{var}} = {{value}};
{%- endif -%}

if: "if ({{condition}}) {{body}} {{els}}"
elif: "else if ({{condition}}) {{body}} {{els}}"
else: "else {{body}}"
ternar: "({{condition}}?{{body}}:{{els}})"
ternary: "({{condition}}?{{body}}:{{els}})"
func: "{%if env.mod_name != 'main' and nl == 0%}export {%endif%}let {{name}} = {{(decorators + [''])|join('(')}}({{args|join(', ')}}) => {{body}}{{')' * decorators|length}};"

return: "return {{value}};"
Expand Down Expand Up @@ -125,3 +125,4 @@ meta:
name: "node"
separator: "> "
ext: "js"
run: "echo {{code}} | node"
11 changes: 8 additions & 3 deletions translators/js/libs/math.tp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
_: &import_el "let {{node.ast.names[0].name}} = Math.{{name}};"

math:
import_code: "{%if alias != 'Math'%}{{alias}} = Math;{%endif%}"
alt_name: Math
Expand All @@ -12,7 +14,7 @@ math:
alt_name: abs
type: float

factorial":
factorial:
type: int

floor:
Expand All @@ -25,10 +27,10 @@ math:

fsum:
args: [x]
code: "{{x()}}.reduce((sum, current)=>{return sum + current},0)"
code: "{{x}}.reduce((sum, current)=>{return sum + current},0)"
type: float

isinf":
isinf:
args: [x]
code: "({{x}} === Infinity)"
type: bool
Expand Down Expand Up @@ -119,13 +121,16 @@ math:
type: float

tau:
import_code: *import_el
code: "(Math.PI*2)"
type: float

e:
import_code: *import_el
alt_name: "E"
type: float

pi:
import_code: *import_el
alt_name: PI
type: float
27 changes: 16 additions & 11 deletions translators/js/libs/react.tp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ Elem:
ret_type: react_el

react_render:
code: |-
{{- env.strings.insert(0, "import React from 'react';\nimport ReactDOM from 'react-dom';\n\n") or ''-}}
ReactDOM.render({{args|join(', ')}})
alt_name: "ReactDOM.render"

ReactDOM:
import_code: "import ReactDOM from 'react-dom';"
type: {module: [React]}

React:
import_code: "import React from 'react';"
type: {module: [React]}

React.useState:
ret_type:
- '{{args[0].type}}'
- function
# - func:
# - 'React.useState'
# - ['{{args[0].type}}']
# - 'None'
use_state: &use_state
import_code: "import {useState} from 'react';"
alt_name: "useState"
ret_type:
- '{{args[0].type}}'
- callable
useState: *use_state

style:
code: "import {{args[0]}};"
7 changes: 7 additions & 0 deletions translators/js/macros.tp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ setattr:
int:
alt_name: 'Number'

float:
alt_name: 'Number'

str:
code: '({{args[0]}}).toString()'

Expand All @@ -65,6 +68,10 @@ js:

# unpacking fields function a({f1, f2}){...}
do:
code: "{{'{'}}{{args|join(', ')}}}"
decorate: >-
{%if env.mod_name != '__main__'%}export {%endif%}let
{{name}} = {{(decorators[1:] + [''])|join('(')}}({{'{'}}{{args|join(', ')}}{{'}'}}) => {{body}}{{')' * (decorators|length - 1)}};

getattr:
code: "{{args[0]}}[{{args[1]}}]"

0 comments on commit 932515f

Please sign in to comment.