diff --git a/compiler/grammar.ypp b/compiler/grammar.ypp index 92f4bc84ef023ddf71422f82dc57bec40ea6cf2a..e75f522d8b4babe4bf5c29e30aa631d49edd913e 100644 --- a/compiler/grammar.ypp +++ b/compiler/grammar.ypp @@ -89,9 +89,12 @@ extern int yyerrorCount; /* %right '!' '~' */ //优先级高 -%type constant_exp number -%type string else_exp -%type signal signal_exp signal_call func_call condition_exp variable_exp compare_exp +%type number +%type signal +%type string +%type signal_call func_call +%type condition_exp compare_exp else_exp +%type signal_exp variable_exp %type arg_list @@ -100,32 +103,19 @@ extern int yyerrorCount; signal: signal_exp {$$ = $1; Compiler_t::getInst().setASTRoot($1); return 0;} /* 信号表达式 */ -signal_exp: constant_exp {$$ = new ASTNumber_t($1);} - |variable_exp +signal_exp: variable_exp |condition_exp - |compare_exp /* 可变表达式, 不是变量表达式, 意思是其值需要在运行期才能求出的表达式 */ variable_exp: "time" {$$ = new ASTTime_t();} |'(' variable_exp ')' {$$ = $2;} |"index" {$$ = new ASTIndex_t();} |"N" {$$ = new ASTN_t();} + |number {$$ = new ASTNumber_t($1);} |func_call |signal_call -/* 可变表达式和常量表达式之间的运算 */ - |variable_exp '+' constant_exp {$$ = new ASTOperator_t('+', $1, new ASTNumber_t($3));} - |variable_exp '-' constant_exp {$$ = new ASTOperator_t('-', $1, new ASTNumber_t($3));} - |variable_exp '*' constant_exp {$$ = new ASTOperator_t('*', $1, new ASTNumber_t($3));} - |variable_exp '/' constant_exp {$$ = new ASTOperator_t('/', $1, new ASTNumber_t($3));} - |variable_exp '%' constant_exp {$$ = new ASTOperator_t('%', $1, new ASTNumber_t($3));} - |variable_exp '^' constant_exp {$$ = new ASTOperator_t('^', $1, new ASTNumber_t($3));} -/* 反方向 */ - |constant_exp '+' variable_exp {$$ = new ASTOperator_t('+', new ASTNumber_t($1), $3);} - |constant_exp '-' variable_exp {$$ = new ASTOperator_t('-', new ASTNumber_t($1), $3);} - |constant_exp '*' variable_exp {$$ = new ASTOperator_t('*', new ASTNumber_t($1), $3);} - |constant_exp '/' variable_exp {$$ = new ASTOperator_t('/', new ASTNumber_t($1), $3);} - |constant_exp '%' variable_exp {$$ = new ASTOperator_t('%', new ASTNumber_t($1), $3);} - |constant_exp '^' variable_exp {$$ = new ASTOperator_t('^', new ASTNumber_t($1), $3);} + |compare_exp + |'-' variable_exp {$$ = new ASTOperator_t('*', $2, new ASTNumber_t(-1));} /* 可变表达式和可变表达式之间的运算 */ |variable_exp '+' variable_exp {$$ = new ASTOperator_t('+', $1, $3);} |variable_exp '-' variable_exp {$$ = new ASTOperator_t('-', $1, $3);} @@ -135,23 +125,8 @@ variable_exp: "time" {$$ = new ASTTime_t();} |variable_exp '^' variable_exp {$$ = new ASTOperator_t('^', $1, $3);} number: "number" - |'-' "number" {$$ = -$2;} - -/* 常量表达式, 不是不可变表达式, 意思是其值在编译期就可求出的表达式 */ -constant_exp: number - |'(' constant_exp ')' {$$ = $2;} - |constant_exp '+' constant_exp {$$ = $1 + $3;} - |constant_exp '-' constant_exp {$$ = $1 - $3;} - |constant_exp '*' constant_exp {$$ = $1 * $3;} - |constant_exp '/' constant_exp {$$ = $1 / $3;} - |constant_exp '%' constant_exp {$$ = std::fmod($1, $3);} - |constant_exp '^' constant_exp {$$ = powf($1, $3);} - |constant_exp '>' constant_exp {$$ = $1 > $3;} - |constant_exp '<' constant_exp {$$ = $1 < $3;} - |constant_exp "==" constant_exp {$$ = $1 == $3;} - |constant_exp "!=" constant_exp {$$ = $1 != $3;} - |constant_exp ">=" constant_exp {$$ = $1 >= $3;} - |constant_exp "<=" constant_exp {$$ = $1 <= $3;} + /* |'(' "number" ')' {$$ = $2;} */ + /* |'-' "number" {$$ = -$2;} */ signal_call: "symbol" { $$ = nullptr; @@ -216,22 +191,6 @@ compare_exp: variable_exp '>' variable_exp {$$ = new ASTOperator_t('>', $1, $3); |variable_exp ">=" variable_exp {$$ = new ASTCompare_t(ASTCompare_t::GEQU, $1, $3);} |variable_exp "<=" variable_exp {$$ = new ASTCompare_t(ASTCompare_t::LEQU, $1, $3);} - |constant_exp '>' variable_exp {$$ = new ASTOperator_t('>', new ASTNumber_t($1), $3);} - |constant_exp '<' variable_exp {$$ = new ASTOperator_t('<', new ASTNumber_t($1), $3);} - |constant_exp "==" variable_exp {$$ = new ASTCompare_t(ASTCompare_t::EQU, new ASTNumber_t($1), $3);} - |constant_exp "!=" variable_exp {$$ = new ASTCompare_t(ASTCompare_t::NEQU, new ASTNumber_t($1), $3);} - |constant_exp ">=" variable_exp {$$ = new ASTCompare_t(ASTCompare_t::GEQU, new ASTNumber_t($1), $3);} - |constant_exp "<=" variable_exp {$$ = new ASTCompare_t(ASTCompare_t::LEQU, new ASTNumber_t($1), $3);} - - |variable_exp '>' constant_exp {$$ = new ASTOperator_t('<', $1, new ASTNumber_t($3));} - |variable_exp '<' constant_exp {$$ = new ASTOperator_t('>', $1, new ASTNumber_t($3));} - |variable_exp "==" constant_exp {$$ = new ASTCompare_t(ASTCompare_t::EQU, $1, new ASTNumber_t($3));} - |variable_exp "!=" constant_exp {$$ = new ASTCompare_t(ASTCompare_t::NEQU, $1, new ASTNumber_t($3));} - |variable_exp ">=" constant_exp {$$ = new ASTCompare_t(ASTCompare_t::GEQU, $1, new ASTNumber_t($3));} - |variable_exp "<=" constant_exp {$$ = new ASTCompare_t(ASTCompare_t::LEQU, $1, new ASTNumber_t($3));} - - |'(' compare_exp ')' {$$ = $2;} - condition_exp: signal_exp "if" compare_exp else_exp {$$ = new ASTCondition_t($3, $1, $4);} | '(' condition_exp ')' {$$ = $2;} diff --git a/lib/basic.cpp b/lib/basic.cpp index f0ea7ff38ec8717c2a9c796d2454c28426c54eac..88615e0c17e8baa9920c6e74a8e3ff2abcd30674 100644 --- a/lib/basic.cpp +++ b/lib/basic.cpp @@ -227,6 +227,16 @@ EXPORT void image(pFunCallArg_t pArgs, float* output) output[i] = arg0[i].image; } +EXPORT void integrate(pFunCallArg_t pArgs, float* output) +{ + int allCalNum = pArgs->allCalNum; + float* arg0 = static_cast(pArgs->args[0]); + + output[0] = arg0[0]; + + for (int i = 1;i < allCalNum;i++) + output[i] = output[i - 1] + arg0[i]; +} LibFunction_t funcs[] = { LIB_FUNCTION(__sin, 1, .name = "sin"), @@ -241,6 +251,7 @@ LibFunction_t funcs[] = { // LIB_FUNCTION(angle, 1), LIB_FUNCTION(conv, 4), LIB_FUNCTION(sum, 1), + LIB_FUNCTION(integrate, 1), LIB_FUNCTION(__sqrt, 1, .name = "sqrt"), LIB_FUNCTION(to_signed, 2), LIB_FUNCTION(to_unsigned, 2), diff --git a/workspace_demo/IQ_modulation.json b/workspace_demo/IQ_modulation.json new file mode 100644 index 0000000000000000000000000000000000000000..180989979ece05fe68bc59efcc0134092e0f7d51 --- /dev/null +++ b/workspace_demo/IQ_modulation.json @@ -0,0 +1,57 @@ +{ + "sample_points": 128, + "sample_rate_num": 5, + "sample_rate_unit": 1, + "signal_array": [ + { + "name": "data_I", + "source_code": "(sin(freq(100))>=0)*2-1", + "spectrum_mode": false + }, + { + "name": "data_Q", + "source_code": "(sin(freq(100))>0.7)*2-1", + "spectrum_mode": false + }, + { + "name": "base_I", + "source_code": "cos(freq(2000))", + "spectrum_mode": false + }, + { + "name": "base_Q", + "source_code": "-sin(freq(2000))", + "spectrum_mode": false + }, + { + "name": "mod_I", + "source_code": "data_I * base_I", + "spectrum_mode": false + }, + { + "name": "mod_Q", + "source_code": "data_Q * base_Q", + "spectrum_mode": false + }, + { + "name": "mod", + "source_code": "mod_I + mod_Q", + "spectrum_mode": false + }, + { + "name": "mod_spec", + "source_code": "length(fft(mod))", + "spectrum_mode": true + }, + { + "name": "demod_I", + "source_code": "mod*base_I", + "spectrum_mode": false + }, + { + "name": "demod_Q", + "source_code": "mod*base_Q", + "spectrum_mode": false + } + ] +}