-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. 增加tm2c目录,专门用于python到C的转换 2. 增加文档说明抽象语法树的结构
- Loading branch information
user
authored and
user
committed
Apr 23, 2016
1 parent
985db73
commit 0809471
Showing
9 changed files
with
650 additions
and
108 deletions.
There are no files selected for viewing
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,110 @@ | ||
# AST结构 | ||
|
||
|
||
## 基本值 | ||
|
||
### 数字 | ||
``` | ||
{ | ||
class = Token, | ||
type = number, | ||
val = numVal | ||
} | ||
``` | ||
|
||
### 字符串 | ||
``` | ||
{ | ||
class= Token, | ||
type = string, | ||
val = strVal | ||
} | ||
``` | ||
### 名称 | ||
|
||
``` | ||
{ | ||
calss = Token, | ||
type = name, | ||
val = nameVal | ||
} | ||
``` | ||
|
||
### None, True, False | ||
``` | ||
{ | ||
class = Token, | ||
type = None | True | False, | ||
val = None | True | False | ||
} | ||
``` | ||
|
||
|
||
### 列表 | ||
``` | ||
列表 = [] | None | ||
``` | ||
|
||
### 字典 | ||
``` | ||
字典 = [ [键, 值]* ] | ||
``` | ||
|
||
## 语句 | ||
``` | ||
语句 = 赋值 | 条件 | 循环 | 基本值 | [语句] | ||
``` | ||
|
||
** 注意 ** 语句可能是列表,需要先使用`isinstance`或者`istype`来判断,然后取type属性判断 | ||
|
||
## 赋值 | ||
|
||
``` | ||
{ | ||
class =AstNode, | ||
type = "=", | ||
first = 语句 | [语句] (多重赋值), | ||
second = 语句 | [语句] , | ||
} | ||
``` | ||
|
||
## 操作符 | ||
``` | ||
{ | ||
class = AstNode, | ||
type = 操作符 +,-,*,/... | ||
first = 操作符左值 | ||
second = 操作符右值 | ||
} | ||
``` | ||
|
||
## if语句 | ||
|
||
``` | ||
{ | ||
class = AstNode, | ||
type =if, | ||
first = 条件, | ||
second = if主体, | ||
third = else语句 | None, | ||
} | ||
``` | ||
|
||
## while 语句 | ||
``` | ||
{ | ||
} | ||
``` | ||
|
||
## for语句 | ||
``` | ||
{ | ||
type = for, | ||
first = for迭代条件, | ||
second = for主体 | ||
} | ||
``` |
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
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,42 @@ | ||
## tm2c.py | ||
|
||
tm2c.py is a converter that converts python source code to C language source code. | ||
|
||
for example, you want to convert the following source code to C. | ||
|
||
``` | ||
# at file hello.py | ||
print("Hello,World") | ||
``` | ||
|
||
just run `tm tm2c.py hello.py > hello.c` or `python tm2c.py hello.py > hello.c` | ||
|
||
then you can get the C code. | ||
|
||
``` | ||
#include "tm.c" | ||
/* DEFINE START */ | ||
Object helloC0; | ||
Object helloC1; | ||
Object helloC2; | ||
Object helloC3; | ||
Object hello0g; | ||
Object helloV__name__; | ||
/* DEFINE END */ | ||
void hello_main(){ | ||
hello0g=dictNew(); | ||
helloC0=stringNew("__main__"); | ||
helloC1=stringNew("__name__"); | ||
helloC2=stringNew("print"); | ||
helloC3=stringNew("hello,world"); | ||
tmDefMod("hello.py", hello0g); | ||
helloV__name__=helloC0; | ||
objSet(hello0g, helloC1, helloV__name__); | ||
tmCall(2, tmGetGlobal(hello0g,helloC2), 1 ,helloC3); | ||
} | ||
int main(int argc, char* argv[]) { | ||
tmRunFunc(argc, argv, "hello.py", hello_main); | ||
} | ||
``` | ||
|
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,2 @@ | ||
|
||
print("hello,world") |
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,10 @@ | ||
# test file for tm2c.py | ||
|
||
# global | ||
_name = "hello" | ||
_a, _b = 1, 2 | ||
|
||
def test(): | ||
name = "hello" | ||
a, b = 1, 2 | ||
|
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 @@ | ||
# test file for tm2c.py | ||
|
||
print("hello,world") | ||
|
||
#@static | ||
def this_is_a_func(): | ||
return "hello" | ||
|
||
_msg = this_is_a_func() |
Oops, something went wrong.