Skip to content

Commit

Permalink
refactor(sfn): refact the sfn
Browse files Browse the repository at this point in the history
please refer to the work log for details
  • Loading branch information
CLimber-Rong committed Jul 12, 2024
1 parent 0b2a2e9 commit a7b9537
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
16 changes: 16 additions & 0 deletions doc/工作日志/20240712.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 2024/07/12 工作日志

本次并没有提交新功能。

### 将SFN涉及到平台的函数封装到了依赖库

``SFN.cpp``将不再直接调用标准C库,而是调用``stmlib.hpp``中的封装后的接口。

### 接下来要做的事

1. 支持文件处理库
2. 支持编译为平面字节码
3. 编写词法分析的保存功能
4. 编写AST的O1优化器
5. 完善标准库
6. 编写AST的解释器
6 changes: 6 additions & 0 deletions include/stdc_implemented/stmlib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#pragma once

#include "String.hpp"
#include "stdlib.h"

/*由于代码经常涉及到基类转派生类,所以我编写了这个可以直接转换的宏*/
/*
Expand Down Expand Up @@ -71,3 +72,8 @@ String toString(bool x) {
#define MACRO_END \
} \
while (0)

#define platform_exit exit
#define platform_system system
#define platform_scanf scanf
#define platform_printf printf
7 changes: 7 additions & 0 deletions include/template/stmlib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ String toString(bool x);
#define MACRO_END \
} \
while (0)

//以下宏需要手动填写

#define platform_exit
#define platform_system
#define platform_scanf
#define platform_printf
17 changes: 7 additions & 10 deletions src/sfn/SFN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

#pragma once

#include"stdio.h"
#include"stdlib.h"

#include"Stamon.hpp"

#define SFN_PARA_LIST \
Expand Down Expand Up @@ -157,7 +154,7 @@ void sfn_puts(SFN_PARA_LIST) {
if(val->getType()!=stamon::datatype::StringTypeID) {
THROW("bad type in puts")
}
printf("%s", ((stamon::datatype::StringType*)(arg->data))->getVal().getstr());
platform_printf("%s", ((stamon::datatype::StringType*)(arg->data))->getVal().getstr());
return;
}

Expand All @@ -166,11 +163,11 @@ void sfn_printNum(SFN_PARA_LIST) {
stamon::datatype::DataType* val = arg->data;

if(val->getType()==stamon::datatype::IntegerTypeID) {
printf("%d", ((stamon::datatype::IntegerType*)val)->getVal());
platform_platform_printf("%d", ((stamon::datatype::IntegerType*)val)->getVal());
} else if(val->getType()==stamon::datatype::FloatTypeID) {
printf("%f", ((stamon::datatype::FloatType*)val)->getVal());
platform_platform_printf("%f", ((stamon::datatype::FloatType*)val)->getVal());
} else if(val->getType()==stamon::datatype::DoubleTypeID) {
printf("%lf", ((stamon::datatype::DoubleType*)val)->getVal());
platform_platform_printf("%lf", ((stamon::datatype::DoubleType*)val)->getVal());
} else {
THROW("bad type in printNum")
}
Expand Down Expand Up @@ -255,7 +252,7 @@ void sfn_input(SFN_PARA_LIST) {

char s[1024];

scanf("%s", s);
platform_scanf("%s", s);

arg->data = manager->MallocObject<stamon::datatype::StringType>(
String(s)
Expand Down Expand Up @@ -297,7 +294,7 @@ void sfn_throw(SFN_PARA_LIST) {

void sfn_system(SFN_PARA_LIST) {
STMException* ex = sfn.ex;
int status = system(
int status = platform_system(
((stamon::datatype::StringType*)arg->data)
->getVal()
.getstr()
Expand All @@ -307,7 +304,7 @@ void sfn_system(SFN_PARA_LIST) {
}

void sfn_exit(SFN_PARA_LIST) {
exit(((stamon::datatype::IntegerType*)arg->data)->getVal());
platform_exit(((stamon::datatype::IntegerType*)arg->data)->getVal());
}

void sfn_version(SFN_PARA_LIST) {
Expand Down

0 comments on commit a7b9537

Please sign in to comment.