Skip to content

Commit

Permalink
1. 增加tm2c目录,专门用于python到C的转换 2. 增加文档说明抽象语法树的结构
Browse files Browse the repository at this point in the history
  • Loading branch information
user authored and user committed Apr 23, 2016
1 parent 985db73 commit 0809471
Show file tree
Hide file tree
Showing 9 changed files with 650 additions and 108 deletions.
110 changes: 110 additions & 0 deletions doc/AST结构.md
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主体
}
```
1 change: 1 addition & 0 deletions src/include/tm.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <time.h>
/* #include <sys/stat.h> */
#include <math.h>
#include <ctype.h>

#define TM_INLINE inline
typedef char instruction;
Expand Down
17 changes: 16 additions & 1 deletion src/ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ Object tmGetGlobal(Object globals, Object okey) {
}

Object tmGetfname(Object fnc) {
if (!IS_FUNC(fnc)) {
if (NOT_FUNC(fnc)) {
tmRaise("tmGetfname expect function");
}
return GET_MODULE(GET_FUNCTION(fnc)->mod)->file;
Expand All @@ -450,6 +450,21 @@ Object tmCall(int lineno, Object func, int args, ...) {
return callFunction(func);
}

Object tmCallNative(int lineno, Object (*fn)(int, va_list), int args, ...) {
int i = 0;
va_list ap;
va_start(ap, args);
argStart();
for (i = 0; i < args; i++) {
argPush(va_arg(ap, Object));
}
va_end(ap);
va_start(ap, args);
Object ret = fn(args, ap);
va_end(ap);
return ret;
}

Object arrayToList(int n, ...) {
va_list ap;
int i = 0;
Expand Down
107 changes: 0 additions & 107 deletions src/tm.c

This file was deleted.

42 changes: 42 additions & 0 deletions tm2c/README.md
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);
}
```

2 changes: 2 additions & 0 deletions tm2c/tests/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

print("hello,world")
10 changes: 10 additions & 0 deletions tm2c/tests/test-assignment.py
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

9 changes: 9 additions & 0 deletions tm2c/tests/test-func.py
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()
Loading

0 comments on commit 0809471

Please sign in to comment.