⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rb_cyc.c

📁 可以在嵌入式应用中
💻 C
字号:
#include <CsAgb.h>
#include <rb_data.h>
#include <rbasic.h>
#include <rb_string.h>
#include <cal.h>
#include <variable.h>
#include <rb_stdio.h>
#include <logic.h>
extern struct comtree *seek_line(u32 line);//寻找指定行
extern void rb_exe_line(char *com);//运行指定整行代码
extern void exp_unfold(char *exp);//删除多余括号
extern void rb_goto(char *exp);
struct cyc *rb_cycs[cyc_max];//循环堆
extern struct comtree *rb_now_line;//当前执行行
void cyc_clear()//清空所有循环体与转子调用
{
   u8 i;
   for (i=0;i<cyc_max;i++)
   {
      if (rb_cycs[i]!=NULL) {free(rb_cycs[i]);rb_cycs[i]=NULL;}
   }
   rb_cycp=0;
}
void pop_cyc()
{
   rb_cycp--;
   free(rb_cycs[rb_cycp]);
   rb_cycs[rb_cycp]=NULL;
}

void push_for(char *exp)
{
   u8 i=0;
   u8 strp;
   u8 loc;
   u8 cycp=rb_cycp;
   char str[str_max_len];
   if (rb_cycp>=cyc_max)
   {
      rb_error=28;
      return;
   }
   if (rb_cycs[cycp]==NULL) rb_cycs[cycp]=(struct cyc*)malloc(sizeof(struct cyc));
   if (rb_cycs[cycp]==NULL)
   {
      rb_error=6;
      return;
   }
   rb_cycs[cycp]->begin=rb_now_line;
   rb_cycp++;
   rb_cycs[cycp]->type=0;//置标志位
   str_up(exp);
   if (rb_cycp>=cyc_max)//椎溢出
   {
      rb_error=28;
      return;
   }
   while (exp[i]!='\0')
   {
      while (exp[i]==' ') i++;
      strp=0;
      while (exp[i]!='=' && exp[i]!='\0')//获得循环变量名
      {
         str[strp]=exp[i];
         strp++;
         i++;
      }
      str[strp]='\0';
      exp_adj(str);
      /*if (check_var_name(str))//变量名非法
      {
         rb_error=22;
         return;
      }*/
      strp=0;
      while(str[strp]!='\0')
      {
         rb_cycs[cycp]->exp[strp]=str[strp];//记录循环变量名
         strp++;
      }
      rb_cycs[cycp]->exp[strp]='\0';
      strp=0;
      i++;
      loc=str_find(exp," TO ");
      if (loc==str_max_len || loc<i)
      {
         rb_error=6;
         return;
      }
      while (i<=loc)//获得循环变量下限
      {
         str[strp]=exp[i];
         i++;
         strp++;
      }
      str[strp]='\0';
      if (strp==0)
      {
         rb_error=6;
         return;
      }
      rb_cycs[cycp]->low=rb_cal(str);//计算下限
      if (rb_error) return;//rb_say_error(rb_cycs[cycp]->exp);
      rb_give_float(rb_cycs[cycp]->exp,rb_cycs[cycp]->low);//循环变量赋初值
      i=loc+4;
      strp=0;
      loc=str_find(exp," STEP ");
      if (loc==str_max_len)//没有找到STEP
      {
         while (exp[i]!='\0')
         {
            if (exp[i]!='\r')
            {
               str[strp]=exp[i];strp++;
            }
            i++;
            
         }
         str[strp]='\0';
         if (strp==0)
         {
            rb_error=6;
            return;
         }
         rb_cycs[cycp]->up=rb_cal(str);
         if (rb_error) return;
         rb_cycs[cycp]->step=rb_cycs[cycp]->up>rb_cycs[cycp]->low?1:-1;//置默认步长
      }
      else//有默认步长
      {
         if (loc<=i)
         {
            rb_error=6;
            return;
         }
         while (i<=loc)
         {
            str[strp]=exp[i];
            i++;
            strp++;
         }
         str[strp]='\0';
         rb_cycs[cycp]->up=rb_cal(str);
         if (rb_error) return;
         i=loc+6;
         strp=0;
         while (exp[i]!='\0')//获得步长
         {
            str[strp]=exp[i];
            i++;
            strp++;
         }
         str[strp]='\0';
         if (strp==0)
         {
            rb_error=6;
            return;
         }
         rb_cycs[cycp]->step=rb_cal(str);
      }
   }

}

void rb_next()
{
   u8 cycp=rb_cycp-1;
   double valu;
   if (rb_cycp<1 || rb_cycs[cycp]->type)
   {
      rb_error=8;
      return;
   }
   valu=rb_get_float(rb_cycs[cycp]->exp)+rb_cycs[cycp]->step;
   rb_give_float(rb_cycs[cycp]->exp,valu);//更新数值
   //show_float(rb_cycs[cycp]->up,RGB(30,10,0));
   //show_float(rb_cycs[cycp]->low,RGB(10,10,30));
   //show_float((rb_cycs[cycp]->up-valu)*(rb_cycs[cycp]->low-valu),RGB(0,30,10));
   if ((rb_cycs[cycp]->up-valu)*(rb_cycs[cycp]->low-valu)>0.0)//循环结束
   {pop_cyc();return;}
   rb_now_line=rb_cycs[cycp]->begin;//返回循环入口
   //rb_say_error(rb_now_line->com);
}
void push_while(char *exp)
{
   u8 i=0;
   if (rb_cycp>=cyc_max)
   {
      rb_error=28;
      return;
   }
   exp_adj(exp);
   logic_exe(exp);
   if (rb_error) return;
   if (rb_cycs[rb_cycp]==NULL) rb_cycs[rb_cycp]=(struct cyc*)malloc(sizeof(struct cyc));
   if (rb_cycs[rb_cycp]==NULL)
   {
      rb_error=6;
      return;
   }
   while(exp[i]!=0)
   {
      rb_cycs[rb_cycp]->exp[i]=exp[i];
      i++;
   }
   rb_cycs[rb_cycp]->exp[i]='\0';
   rb_cycs[rb_cycp]->type=1;
   rb_cycs[rb_cycp]->begin=rb_now_line;
   rb_cycp++;
}
void rb_wend()
{
   u8 cycp=rb_cycp-1;
   if (rb_cycp<1 || rb_cycs[cycp]->type!=1)
   {
      rb_error=10;
      return;
   }
   if (logic_exe(rb_cycs[cycp]->exp)) rb_now_line=rb_cycs[cycp]->begin;
   else pop_cyc();
}
void push_gosub(char *exp)
{
   double line;
   struct comtree *sub;
   char com[str_max_len];
   u8 i=0;
   line=rb_cal(exp);
   if (rb_error) return;
   if (rb_cycp>=cyc_max)
   {
      rb_error=28;
      return;
   }
   if (rb_cycs[rb_cycp]==NULL) rb_cycs[rb_cycp]=(struct cyc*)malloc(sizeof(struct cyc));
   if (rb_cycs[rb_cycp]==NULL)
   {
      rb_error=6;
      return;
   }
   rb_cycs[rb_cycp]->begin=rb_now_line;
   rb_cycs[rb_cycp]->type=2;
   rb_cycp++;
   sub=seek_line(line);//寻找指定行
   if (sub==NULL)//找不到行号
   {
      rb_error=15;
      return;
   }
   while (sub->com[i]!='\r' && sub->com[i]!='\0')
   {
      com[i]=sub->com[i];
      i++;
   }
   com[i]='\0';
   rb_now_line=sub;
   rb_exe_line(com);//持行子式第一行
}
void rb_ret()
{
   if (rb_cycp<1 || rb_cycs[rb_cycp-1]->type!=2)
   {
      rb_error=10;
      return;
   }
   rb_now_line=rb_cycs[rb_cycp-1]->begin;
   pop_cyc();
}
void rb_if(char *exp)
{
   char str[str_max_len];
   u8 i,strp;
   u8 loc;
   u8 flag;
   str_up(exp);
   loc=str_find(exp," THEN ");
   if (loc==str_max_len)
   {
      rb_error=6;
      return;
   }
   i=0;
   while(i<loc)
   {
      str[i]=exp[i];
      i++;
   }
   str[i]='\0';
   i=loc+6;
   flag=logic_exe(str);
   if (rb_error) return;
   loc=str_find(exp," ELSE ");
   if (flag)
   {
      strp=0;
      while(i<loc && exp[i]!='\0')
      {
         str[strp]=exp[i];
         i++;
         strp++;
      }
      if (strp==0)
      {
         rb_error=6;
         return;
      }
      str[strp]='\0';
      exp_unfold(str);//删除多余括号
      rb_exe_line(str);
      return;
   }
   if (loc==str_max_len) return;
   i=loc+6;
   strp=0;
   while (exp[i]!='\0')
   {
      str[strp]=exp[i];
      i++;
      strp++;
   }
   if (strp==0)
   {
      rb_error=6;
      return;
   }
   str[strp]='\0';
   exp_unfold(str);
   rb_exe_line(str);
}
void rb_on(char *exp)
{
   u8 i,strp,loc,type=0,count=0;
   double n;
   char str[str_max_len];
   str_up(exp);
   loc=str_find(exp," GOSUB ");
   if (loc==str_max_len) loc=str_find(exp," GOTO ");
   else type=1;
   if (loc==str_max_len)
   {
      rb_error=6;
      return;
   }
   i=0;
   while (i<loc)
   {
      str[i]=exp[i];
      i++;
   }
   if (i==0)
   {
      rb_error=6;
      return;
   }
   str[i]='\0';
   n=rb_cal(str);
   if (rb_error) return;
   if (n<1 || n>10) return;//超出范围
   n=(int)n;
   i=loc+6+type;
   while (exp[i]==' ') i++;
   strp=0;
   while (exp[i]!='\0')
   {
      while (is_number(exp[i]))
      {
         str[strp]=exp[i];
         i++;
         strp++;
      }
      while (exp[i]==' ') i++;
      if (strp==0 || (exp[i]!=',' && exp[i]!='\0'))
      {
         rb_error=6;
         return;
      }
      if (exp[i]!='\0') i++;
      str[strp]='\0';
      count++;
      if (count==n)
      {
         if (type) push_gosub(str);
         else rb_goto(str);
      }
      else
      {
         strp=0;
         while (exp[i]==' ') i++;
      }
   }
}
   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -