summaryrefslogtreecommitdiff
path: root/src/lang/flow.cc
blob: c7c70a0aa9fbd48f00e78f728adf0424c6e1f630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <lang/env.h>
#include <mt/dates.h>
using namespace std;

TOK_IMPL(branch) {
  int index=atoi(env[0]);
  if(index<0) index+=env.args;
  if(index>0&&index<env.args) env.parg(out,index);
  else if(index<0||index>=env.args) THROW("htc: bad branching index "<<index);
}

TOK_IMPL(if) {
  if(atob(env[0])) env.parg(out,1); else env.parg(out,2);
}

TOK_IMPL(nif) {
  if(!atob(env[0])) env.parg(out,1); else env.parg(out,2);
}

TOK_IMPL(switch) {
  sref s=env[0];
  int i;
  for(i=1;i+1<env.args;i+=2) if(env.parg(i)==s) { env.parg(out,i+1); return; }
  if(i<env.args) env.parg(out,i);
}

/////////////////////////////////////////////////////////////////////

static int eval(Env& env,expr_t* x,int mod) {
  mswrite out;
  if(x->status&OP_PARSE) env.parse(out,x->expr); else x->eval(out,env,mod);
  return atob(out.str());
}

TOK_IMPL(for) {
  expr_t* x[4]={env.arg(0),env.arg(1),env.arg(2),env.arg(3)};
  Env nenv(env);
  for(eval(nenv,x[0],OP_ALL);
      env.req->is_running()&&eval(nenv,x[1],OP_LOCAL);
      eval(nenv,x[2],OP_LOCAL))
    nenv.parse(out,x[3]);
}

TOK_IMPL(while) {
  expr_t* x[2]={env.arg(0),env.arg(1)};
  Env nenv(env);
  while(env.req->is_running()&&eval(nenv,x[0],OP_ALL)) nenv.parse(out,x[1]);
}

/////////////////////////////////////////////////////////////////////

TOK_IMPL(forall) {
  mstring sep; env.parg(sep,-1);
  sref s=env[0];
  sref p,e=env[1];
  expr_t* x=env.arg(2);
  Env nenv(env);
  if(sep.nempty()) {
    charset seps(sep.c_str());
    while(Split(p,e,seps)) {
      nenv.aloc(s,p);
      nenv.parse(out,x);
    }
  }
  else {
    while(Split(p,e)) {
      nenv.aloc(s,p);
      nenv.parse(out,x);
    }
  }
}

/////////////////////////////////////////////////////////////////////

TOK_IMPL(on_arg) {
  if(env.up&&env.up->onarg(atoi(env[0])-1)) env.parg(out,1); else env.parg(out,2);
}
TOK_IMPL(on_glob) { if(env.onglob(env[0])) env.parg(out,1); else env.parg(out,2); }
TOK_IMPL(on_loc) { if(env.onloc(env[0])) env.parg(out,1); else env.parg(out,2); }

/////////////////////////////////////////////////////////////////////

TOK_IMPL(setlang) {
  env.req->lang=Language(env[0]); if(env.req->lang<0) env.req->lang=0;
}

TOK_IMPL(lang) {
  env.parg(out,env.req->lang);
}  

/////////////////////////////////////////////////////////////////////

void envAddFlow(tokmap& T)
{
  TOK_ADD(branch,"x*",tok_t::PUBLIC,
          "%Multiple branch\n"
          "%Syntax: \\branch{x}<body>..<body>\n"
          "%\n"
          "%x==0 means nothing is executed, the first body for x==1, etc\n"
          "%Negative values wrap to end\n"
          );
  TOK_ADD(if,"xr?",tok_t::PUBLIC,
          "%if - logical branch\n"
          "%Syntax: \\if{x}{body}<else>\n"
          "%\n"
          "%if x is non-zero, evaluates body, otherwise else if exist\n"
          );
  TOK_ADD(nif,"xr?",tok_t::PUBLIC,
          "%nif - logical branch\n"
          "%Syntax: \\nif{x}{body}<else>\n"
          "%\n"
          "%if x is zero, evaluates body, otherwise else if exist\n"
          );
  TOK_ADD(switch,"x*",tok_t::PUBLIC,
          "%Multiple switch\n"
          "%Syntax: \\switch{x}<val><body>..<val><body>..<default>\n"
          "%\n"
          "%A sequence of pairs <val><body> follows x\n"
          "%x==val means body is executed\n"
          "%\n"
          "%An unpaired body at the end is treated as default\n"
          );

  /////////////////////////////////////////////////////////////////////

  TOK_ADD(for,"rrrr",tok_t::PRIVATE,
          "%C style\n"
          "%Syntax: \\for{start}{test}{next}{body}\n"
          "%\n"
          "%Note that test and next are evaluated with optimization: only\n"
          "%local variables are updated. Hence, you cannot use a function\n"
          "%as the whole test, only as a part of the test, as in i<\\function.\n"
          );
  TOK_ADD(while,"rr",tok_t::PRIVATE,
          "%C style\n"
          "%Syntax: \\while{test}{body}\n"
          "%\n"
          "%There is no optimization on test - globals are re-evaluated in each step\n"
          );
  TOK_ADD(forall,"xx?r",tok_t::PUBLIC,
          "%Loop through elements\n"
          "%Syntax: \\forall{varname}{list}<additional whitespace>{body}\n"
          "%\n"
          "%Default whitespace are ( \\t\\r\\n). Optional whitespace characters\n"
          "%may be given. This enables the design of special lists.\n"
          );

  /////////////////////////////////////////////////////////////////////

  TOK_ADD(on_arg,"xr?",tok_t::PUBLIC,
          "%Do if arg exist and is non-empty\n"
          "%Syntax: \\on.arg{number}{body}<else>\n"
          );
  TOK_ADD(on_glob,"xr?",tok_t::PUBLIC,
          "%Do if global variable exist and is non-empty\n"
          "%Syntax: \\on.glob{varname}{body}<else>\n"
          );
  TOK_ADD(on_loc,"xr?",tok_t::PUBLIC,
          "%Do if local variable exist and is non-empty\n"
          "%Syntax: \\on.loc{varname}{body}<else>\n"
          );

  /////////////////////////////////////////////////////////////////////

  TOK_ADD(lang,"r*",tok_t::PUBLIC,
          "%Branch on global language\n"
          "%Syntax: \\lang{en}{sv}..\n"
          "%\n"
          "%Currently English (en) and Swedish (sv) are supported\n"
          );
  TOK_ADD(setlang,"x",tok_t::PUBLIC,
          "%Set global language\n"
          "%Syntax: \\setlang{language code list}\n"
          "%\n"
          "%The language actually set is a negotitation -\n"
          "%the first code in the list recognized is set.\n"
          "%If not found, the default language English (en) is set.\n"
          );
}