expr.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ctype.h>
  5. #include <mach.h>
  6. #define Extern extern
  7. #include "acid.h"
  8. static int fsize[] =
  9. {
  10. ['A'] 4,
  11. ['B'] 4,
  12. ['C'] 1,
  13. ['D'] 4,
  14. ['F'] 8,
  15. ['G'] 8,
  16. ['O'] 4,
  17. ['Q'] 4,
  18. ['R'] 4,
  19. ['S'] 4,
  20. ['U'] 4,
  21. ['V'] 8,
  22. ['W'] 8,
  23. ['X'] 4,
  24. ['Y'] 8,
  25. ['Z'] 8,
  26. ['a'] 4,
  27. ['b'] 1,
  28. ['c'] 1,
  29. ['d'] 2,
  30. ['f'] 4,
  31. ['g'] 4,
  32. ['o'] 2,
  33. ['q'] 2,
  34. ['r'] 2,
  35. ['s'] 4,
  36. ['u'] 2,
  37. ['x'] 2,
  38. ['3'] 10,
  39. ['8'] 10,
  40. };
  41. int
  42. fmtsize(Value *v)
  43. {
  44. int ret;
  45. switch(v->fmt) {
  46. default:
  47. return fsize[v->fmt];
  48. case 'i':
  49. case 'I':
  50. if(v->type != TINT || machdata == 0)
  51. error("no size for i fmt pointer ++/--");
  52. ret = (*machdata->instsize)(cormap, v->ival);
  53. if(ret < 0) {
  54. ret = (*machdata->instsize)(symmap, v->ival);
  55. if(ret < 0)
  56. error("%r");
  57. }
  58. return ret;
  59. }
  60. }
  61. void
  62. chklval(Node *lp)
  63. {
  64. if(lp->op != ONAME)
  65. error("need l-value");
  66. }
  67. void
  68. olist(Node *n, Node *res)
  69. {
  70. expr(n->left, res);
  71. expr(n->right, res);
  72. }
  73. void
  74. oeval(Node *n, Node *res)
  75. {
  76. expr(n->left, res);
  77. if(res->type != TCODE)
  78. error("bad type for eval");
  79. expr(res->cc, res);
  80. }
  81. void
  82. ocast(Node *n, Node *res)
  83. {
  84. if(n->sym->lt == 0)
  85. error("%s is not a complex type", n->sym->name);
  86. expr(n->left, res);
  87. res->comt = n->sym->lt;
  88. res->fmt = 'a';
  89. }
  90. void
  91. oindm(Node *n, Node *res)
  92. {
  93. Map *m;
  94. Node l;
  95. m = cormap;
  96. if(m == 0)
  97. m = symmap;
  98. expr(n->left, &l);
  99. if(l.type != TINT)
  100. error("bad type for *");
  101. if(m == 0)
  102. error("no map for *");
  103. indir(m, l.ival, l.fmt, res);
  104. res->comt = l.comt;
  105. }
  106. void
  107. oindc(Node *n, Node *res)
  108. {
  109. Map *m;
  110. Node l;
  111. m = symmap;
  112. if(m == 0)
  113. m = cormap;
  114. expr(n->left, &l);
  115. if(l.type != TINT)
  116. error("bad type for @");
  117. if(m == 0)
  118. error("no map for @");
  119. indir(m, l.ival, l.fmt, res);
  120. res->comt = l.comt;
  121. }
  122. void
  123. oframe(Node *n, Node *res)
  124. {
  125. char *p;
  126. Node *lp;
  127. uvlong ival;
  128. Frtype *f;
  129. p = n->sym->name;
  130. while(*p && *p == '$')
  131. p++;
  132. lp = n->left;
  133. if(localaddr(cormap, p, lp->sym->name, &ival, rget) < 0)
  134. error("colon: %r");
  135. res->ival = ival;
  136. res->op = OCONST;
  137. res->fmt = 'X';
  138. res->type = TINT;
  139. /* Try and set comt */
  140. for(f = n->sym->local; f; f = f->next) {
  141. if(f->var == lp->sym) {
  142. res->comt = f->type;
  143. res->fmt = 'a';
  144. break;
  145. }
  146. }
  147. }
  148. void
  149. oindex(Node *n, Node *res)
  150. {
  151. Node l, r;
  152. expr(n->left, &l);
  153. expr(n->right, &r);
  154. if(r.type != TINT)
  155. error("bad type for []");
  156. switch(l.type) {
  157. default:
  158. error("lhs[] has bad type");
  159. case TINT:
  160. indir(cormap, l.ival+(r.ival*fsize[l.fmt]), l.fmt, res);
  161. res->comt = l.comt;
  162. res->fmt = l.fmt;
  163. break;
  164. case TLIST:
  165. nthelem(l.l, r.ival, res);
  166. break;
  167. case TSTRING:
  168. res->ival = 0;
  169. if(r.ival >= 0 && r.ival < l.string->len) {
  170. int xx8; /* to get around bug in vc */
  171. xx8 = r.ival;
  172. res->ival = l.string->string[xx8];
  173. }
  174. res->op = OCONST;
  175. res->type = TINT;
  176. res->fmt = 'c';
  177. break;
  178. }
  179. }
  180. void
  181. oappend(Node *n, Node *res)
  182. {
  183. Value *v;
  184. Node r, l;
  185. int empty;
  186. expr(n->left, &l);
  187. expr(n->right, &r);
  188. if(l.type != TLIST)
  189. error("must append to list");
  190. empty = (l.l == nil && (n->left->op == ONAME));
  191. append(res, &l, &r);
  192. if(empty) {
  193. v = n->left->sym->v;
  194. v->type = res->type;
  195. v->Store = res->Store;
  196. v->comt = res->comt;
  197. }
  198. }
  199. void
  200. odelete(Node *n, Node *res)
  201. {
  202. Node l, r;
  203. expr(n->left, &l);
  204. expr(n->right, &r);
  205. if(l.type != TLIST)
  206. error("must delete from list");
  207. if(r.type != TINT)
  208. error("delete index must be integer");
  209. delete(l.l, r.ival, res);
  210. }
  211. void
  212. ohead(Node *n, Node *res)
  213. {
  214. Node l;
  215. expr(n->left, &l);
  216. if(l.type != TLIST)
  217. error("head needs list");
  218. res->op = OCONST;
  219. if(l.l) {
  220. res->type = l.l->type;
  221. res->Store = l.l->Store;
  222. }
  223. else {
  224. res->type = TLIST;
  225. res->l = 0;
  226. }
  227. }
  228. void
  229. otail(Node *n, Node *res)
  230. {
  231. Node l;
  232. expr(n->left, &l);
  233. if(l.type != TLIST)
  234. error("tail needs list");
  235. res->op = OCONST;
  236. res->type = TLIST;
  237. if(l.l)
  238. res->l = l.l->next;
  239. else
  240. res->l = 0;
  241. }
  242. void
  243. oconst(Node *n, Node *res)
  244. {
  245. res->op = OCONST;
  246. res->type = n->type;
  247. res->Store = n->Store;
  248. res->comt = n->comt;
  249. }
  250. void
  251. oname(Node *n, Node *res)
  252. {
  253. Value *v;
  254. v = n->sym->v;
  255. if(v->set == 0)
  256. error("%s used but not set", n->sym->name);
  257. res->op = OCONST;
  258. res->type = v->type;
  259. res->Store = v->Store;
  260. res->comt = v->comt;
  261. }
  262. void
  263. octruct(Node *n, Node *res)
  264. {
  265. res->op = OCONST;
  266. res->type = TLIST;
  267. res->l = construct(n->left);
  268. }
  269. void
  270. oasgn(Node *n, Node *res)
  271. {
  272. Node *lp, r;
  273. Value *v;
  274. lp = n->left;
  275. switch(lp->op) {
  276. case OINDM:
  277. windir(cormap, lp->left, n->right, res);
  278. break;
  279. case OINDC:
  280. windir(symmap, lp->left, n->right, res);
  281. break;
  282. default:
  283. chklval(lp);
  284. v = lp->sym->v;
  285. expr(n->right, &r);
  286. v->set = 1;
  287. v->type = r.type;
  288. v->Store = r.Store;
  289. res->op = OCONST;
  290. res->type = v->type;
  291. res->Store = v->Store;
  292. res->comt = v->comt;
  293. }
  294. }
  295. void
  296. oadd(Node *n, Node *res)
  297. {
  298. Node l, r;
  299. if(n->right == nil){ /* unary + */
  300. expr(n->left, res);
  301. return;
  302. }
  303. expr(n->left, &l);
  304. expr(n->right, &r);
  305. res->fmt = l.fmt;
  306. res->op = OCONST;
  307. res->type = TFLOAT;
  308. switch(l.type) {
  309. default:
  310. error("bad lhs type +");
  311. case TINT:
  312. switch(r.type) {
  313. case TINT:
  314. res->type = TINT;
  315. res->ival = l.ival+r.ival;
  316. break;
  317. case TFLOAT:
  318. res->fval = l.ival+r.fval;
  319. break;
  320. default:
  321. error("bad rhs type +");
  322. }
  323. break;
  324. case TFLOAT:
  325. switch(r.type) {
  326. case TINT:
  327. res->fval = l.fval+r.ival;
  328. break;
  329. case TFLOAT:
  330. res->fval = l.fval+r.fval;
  331. break;
  332. default:
  333. error("bad rhs type +");
  334. }
  335. break;
  336. case TSTRING:
  337. if(r.type == TSTRING) {
  338. res->type = TSTRING;
  339. res->fmt = 's';
  340. res->string = stradd(l.string, r.string);
  341. break;
  342. }
  343. if(r.type == TINT) {
  344. res->type = TSTRING;
  345. res->fmt = 's';
  346. res->string = straddrune(l.string, r.ival);
  347. break;
  348. }
  349. error("bad rhs for +");
  350. case TLIST:
  351. res->type = TLIST;
  352. switch(r.type) {
  353. case TLIST:
  354. res->l = addlist(l.l, r.l);
  355. break;
  356. default:
  357. r.left = 0;
  358. r.right = 0;
  359. res->l = addlist(l.l, construct(&r));
  360. break;
  361. }
  362. }
  363. }
  364. void
  365. osub(Node *n, Node *res)
  366. {
  367. Node l, r;
  368. expr(n->left, &l);
  369. expr(n->right, &r);
  370. res->fmt = l.fmt;
  371. res->op = OCONST;
  372. res->type = TFLOAT;
  373. switch(l.type) {
  374. default:
  375. error("bad lhs type -");
  376. case TINT:
  377. switch(r.type) {
  378. case TINT:
  379. res->type = TINT;
  380. res->ival = l.ival-r.ival;
  381. break;
  382. case TFLOAT:
  383. res->fval = l.ival-r.fval;
  384. break;
  385. default:
  386. error("bad rhs type -");
  387. }
  388. break;
  389. case TFLOAT:
  390. switch(r.type) {
  391. case TINT:
  392. res->fval = l.fval-r.ival;
  393. break;
  394. case TFLOAT:
  395. res->fval = l.fval-r.fval;
  396. break;
  397. default:
  398. error("bad rhs type -");
  399. }
  400. break;
  401. }
  402. }
  403. void
  404. omul(Node *n, Node *res)
  405. {
  406. Node l, r;
  407. expr(n->left, &l);
  408. expr(n->right, &r);
  409. res->fmt = l.fmt;
  410. res->op = OCONST;
  411. res->type = TFLOAT;
  412. switch(l.type) {
  413. default:
  414. error("bad lhs type *");
  415. case TINT:
  416. switch(r.type) {
  417. case TINT:
  418. res->type = TINT;
  419. res->ival = l.ival*r.ival;
  420. break;
  421. case TFLOAT:
  422. res->fval = l.ival*r.fval;
  423. break;
  424. default:
  425. error("bad rhs type *");
  426. }
  427. break;
  428. case TFLOAT:
  429. switch(r.type) {
  430. case TINT:
  431. res->fval = l.fval*r.ival;
  432. break;
  433. case TFLOAT:
  434. res->fval = l.fval*r.fval;
  435. break;
  436. default:
  437. error("bad rhs type *");
  438. }
  439. break;
  440. }
  441. }
  442. void
  443. odiv(Node *n, Node *res)
  444. {
  445. Node l, r;
  446. expr(n->left, &l);
  447. expr(n->right, &r);
  448. res->fmt = l.fmt;
  449. res->op = OCONST;
  450. res->type = TFLOAT;
  451. switch(l.type) {
  452. default:
  453. error("bad lhs type /");
  454. case TINT:
  455. switch(r.type) {
  456. case TINT:
  457. res->type = TINT;
  458. if(r.ival == 0)
  459. error("zero divide");
  460. res->ival = l.ival/r.ival;
  461. break;
  462. case TFLOAT:
  463. if(r.fval == 0)
  464. error("zero divide");
  465. res->fval = l.ival/r.fval;
  466. break;
  467. default:
  468. error("bad rhs type /");
  469. }
  470. break;
  471. case TFLOAT:
  472. switch(r.type) {
  473. case TINT:
  474. res->fval = l.fval/r.ival;
  475. break;
  476. case TFLOAT:
  477. res->fval = l.fval/r.fval;
  478. break;
  479. default:
  480. error("bad rhs type /");
  481. }
  482. break;
  483. }
  484. }
  485. void
  486. omod(Node *n, Node *res)
  487. {
  488. Node l, r;
  489. expr(n->left, &l);
  490. expr(n->right, &r);
  491. res->fmt = l.fmt;
  492. res->op = OCONST;
  493. res->type = TINT;
  494. if(l.type != TINT || r.type != TINT)
  495. error("bad expr type %");
  496. res->ival = l.ival%r.ival;
  497. }
  498. void
  499. olsh(Node *n, Node *res)
  500. {
  501. Node l, r;
  502. expr(n->left, &l);
  503. expr(n->right, &r);
  504. res->fmt = l.fmt;
  505. res->op = OCONST;
  506. res->type = TINT;
  507. if(l.type != TINT || r.type != TINT)
  508. error("bad expr type <<");
  509. res->ival = l.ival<<r.ival;
  510. }
  511. void
  512. orsh(Node *n, Node *res)
  513. {
  514. Node l, r;
  515. expr(n->left, &l);
  516. expr(n->right, &r);
  517. res->fmt = l.fmt;
  518. res->op = OCONST;
  519. res->type = TINT;
  520. if(l.type != TINT || r.type != TINT)
  521. error("bad expr type >>");
  522. res->ival = (uvlong)l.ival>>r.ival;
  523. }
  524. void
  525. olt(Node *n, Node *res)
  526. {
  527. Node l, r;
  528. expr(n->left, &l);
  529. expr(n->right, &r);
  530. res->fmt = l.fmt;
  531. res->op = OCONST;
  532. res->type = TINT;
  533. switch(l.type) {
  534. default:
  535. error("bad lhs type <");
  536. case TINT:
  537. switch(r.type) {
  538. case TINT:
  539. res->ival = l.ival < r.ival;
  540. break;
  541. case TFLOAT:
  542. res->ival = l.ival < r.fval;
  543. break;
  544. default:
  545. error("bad rhs type <");
  546. }
  547. break;
  548. case TFLOAT:
  549. switch(r.type) {
  550. case TINT:
  551. res->ival = l.fval < r.ival;
  552. break;
  553. case TFLOAT:
  554. res->ival = l.fval < r.fval;
  555. break;
  556. default:
  557. error("bad rhs type <");
  558. }
  559. break;
  560. }
  561. }
  562. void
  563. ogt(Node *n, Node *res)
  564. {
  565. Node l, r;
  566. expr(n->left, &l);
  567. expr(n->right, &r);
  568. res->fmt = 'D';
  569. res->op = OCONST;
  570. res->type = TINT;
  571. switch(l.type) {
  572. default:
  573. error("bad lhs type >");
  574. case TINT:
  575. switch(r.type) {
  576. case TINT:
  577. res->ival = l.ival > r.ival;
  578. break;
  579. case TFLOAT:
  580. res->ival = l.ival > r.fval;
  581. break;
  582. default:
  583. error("bad rhs type >");
  584. }
  585. break;
  586. case TFLOAT:
  587. switch(r.type) {
  588. case TINT:
  589. res->ival = l.fval > r.ival;
  590. break;
  591. case TFLOAT:
  592. res->ival = l.fval > r.fval;
  593. break;
  594. default:
  595. error("bad rhs type >");
  596. }
  597. break;
  598. }
  599. }
  600. void
  601. oleq(Node *n, Node *res)
  602. {
  603. Node l, r;
  604. expr(n->left, &l);
  605. expr(n->right, &r);
  606. res->fmt = 'D';
  607. res->op = OCONST;
  608. res->type = TINT;
  609. switch(l.type) {
  610. default:
  611. error("bad expr type <=");
  612. case TINT:
  613. switch(r.type) {
  614. case TINT:
  615. res->ival = l.ival <= r.ival;
  616. break;
  617. case TFLOAT:
  618. res->ival = l.ival <= r.fval;
  619. break;
  620. default:
  621. error("bad expr type <=");
  622. }
  623. break;
  624. case TFLOAT:
  625. switch(r.type) {
  626. case TINT:
  627. res->ival = l.fval <= r.ival;
  628. break;
  629. case TFLOAT:
  630. res->ival = l.fval <= r.fval;
  631. break;
  632. default:
  633. error("bad expr type <=");
  634. }
  635. break;
  636. }
  637. }
  638. void
  639. ogeq(Node *n, Node *res)
  640. {
  641. Node l, r;
  642. expr(n->left, &l);
  643. expr(n->right, &r);
  644. res->fmt = 'D';
  645. res->op = OCONST;
  646. res->type = TINT;
  647. switch(l.type) {
  648. default:
  649. error("bad lhs type >=");
  650. case TINT:
  651. switch(r.type) {
  652. case TINT:
  653. res->ival = l.ival >= r.ival;
  654. break;
  655. case TFLOAT:
  656. res->ival = l.ival >= r.fval;
  657. break;
  658. default:
  659. error("bad rhs type >=");
  660. }
  661. break;
  662. case TFLOAT:
  663. switch(r.type) {
  664. case TINT:
  665. res->ival = l.fval >= r.ival;
  666. break;
  667. case TFLOAT:
  668. res->ival = l.fval >= r.fval;
  669. break;
  670. default:
  671. error("bad rhs type >=");
  672. }
  673. break;
  674. }
  675. }
  676. void
  677. oeq(Node *n, Node *res)
  678. {
  679. Node l, r;
  680. expr(n->left, &l);
  681. expr(n->right, &r);
  682. res->fmt = 'D';
  683. res->op = OCONST;
  684. res->type = TINT;
  685. res->ival = 0;
  686. switch(l.type) {
  687. default:
  688. break;
  689. case TINT:
  690. switch(r.type) {
  691. case TINT:
  692. res->ival = l.ival == r.ival;
  693. break;
  694. case TFLOAT:
  695. res->ival = l.ival == r.fval;
  696. break;
  697. default:
  698. break;
  699. }
  700. break;
  701. case TFLOAT:
  702. switch(r.type) {
  703. case TINT:
  704. res->ival = l.fval == r.ival;
  705. break;
  706. case TFLOAT:
  707. res->ival = l.fval == r.fval;
  708. break;
  709. default:
  710. break;
  711. }
  712. break;
  713. case TSTRING:
  714. if(r.type == TSTRING) {
  715. res->ival = scmp(r.string, l.string);
  716. break;
  717. }
  718. break;
  719. case TLIST:
  720. if(r.type == TLIST) {
  721. res->ival = listcmp(l.l, r.l);
  722. break;
  723. }
  724. break;
  725. }
  726. if(n->op == ONEQ)
  727. res->ival = !res->ival;
  728. }
  729. void
  730. oland(Node *n, Node *res)
  731. {
  732. Node l, r;
  733. expr(n->left, &l);
  734. expr(n->right, &r);
  735. res->fmt = l.fmt;
  736. res->op = OCONST;
  737. res->type = TINT;
  738. if(l.type != TINT || r.type != TINT)
  739. error("bad expr type &");
  740. res->ival = l.ival&r.ival;
  741. }
  742. void
  743. oxor(Node *n, Node *res)
  744. {
  745. Node l, r;
  746. expr(n->left, &l);
  747. expr(n->right, &r);
  748. res->fmt = l.fmt;
  749. res->op = OCONST;
  750. res->type = TINT;
  751. if(l.type != TINT || r.type != TINT)
  752. error("bad expr type ^");
  753. res->ival = l.ival^r.ival;
  754. }
  755. void
  756. olor(Node *n, Node *res)
  757. {
  758. Node l, r;
  759. expr(n->left, &l);
  760. expr(n->right, &r);
  761. res->fmt = l.fmt;
  762. res->op = OCONST;
  763. res->type = TINT;
  764. if(l.type != TINT || r.type != TINT)
  765. error("bad expr type |");
  766. res->ival = l.ival|r.ival;
  767. }
  768. void
  769. ocand(Node *n, Node *res)
  770. {
  771. Node l, r;
  772. res->op = OCONST;
  773. res->type = TINT;
  774. res->ival = 0;
  775. res->fmt = 'D';
  776. expr(n->left, &l);
  777. if(bool(&l) == 0)
  778. return;
  779. expr(n->right, &r);
  780. if(bool(&r) == 0)
  781. return;
  782. res->ival = 1;
  783. }
  784. void
  785. onot(Node *n, Node *res)
  786. {
  787. Node l;
  788. res->op = OCONST;
  789. res->type = TINT;
  790. res->ival = 0;
  791. expr(n->left, &l);
  792. res->fmt = l.fmt;
  793. if(bool(&l) == 0)
  794. res->ival = 1;
  795. }
  796. void
  797. ocor(Node *n, Node *res)
  798. {
  799. Node l, r;
  800. res->op = OCONST;
  801. res->type = TINT;
  802. res->ival = 0;
  803. res->fmt = 'D';
  804. expr(n->left, &l);
  805. if(bool(&l)) {
  806. res->ival = 1;
  807. return;
  808. }
  809. expr(n->right, &r);
  810. if(bool(&r)) {
  811. res->ival = 1;
  812. return;
  813. }
  814. }
  815. void
  816. oeinc(Node *n, Node *res)
  817. {
  818. Value *v;
  819. chklval(n->left);
  820. v = n->left->sym->v;
  821. res->op = OCONST;
  822. res->type = v->type;
  823. switch(v->type) {
  824. case TINT:
  825. if(n->op == OEDEC)
  826. v->ival -= fmtsize(v);
  827. else
  828. v->ival += fmtsize(v);
  829. break;
  830. case TFLOAT:
  831. if(n->op == OEDEC)
  832. v->fval--;
  833. else
  834. v->fval++;
  835. break;
  836. default:
  837. error("bad type for pre --/++");
  838. }
  839. res->Store = v->Store;
  840. }
  841. void
  842. opinc(Node *n, Node *res)
  843. {
  844. Value *v;
  845. chklval(n->left);
  846. v = n->left->sym->v;
  847. res->op = OCONST;
  848. res->type = v->type;
  849. res->Store = v->Store;
  850. switch(v->type) {
  851. case TINT:
  852. if(n->op == OPDEC)
  853. v->ival -= fmtsize(v);
  854. else
  855. v->ival += fmtsize(v);
  856. break;
  857. case TFLOAT:
  858. if(n->op == OPDEC)
  859. v->fval--;
  860. else
  861. v->fval++;
  862. break;
  863. default:
  864. error("bad type for post --/++");
  865. }
  866. }
  867. void
  868. ocall(Node *n, Node *res)
  869. {
  870. Lsym *s;
  871. Rplace *rsav;
  872. res->op = OCONST; /* Default return value */
  873. res->type = TLIST;
  874. res->l = 0;
  875. chklval(n->left);
  876. s = n->left->sym;
  877. if(n->builtin && !s->builtin){
  878. error("no builtin %s", s->name);
  879. return;
  880. }
  881. if(s->builtin && (n->builtin || s->proc == 0)) {
  882. (*s->builtin)(res, n->right);
  883. return;
  884. }
  885. if(s->proc == 0)
  886. error("no function %s", s->name);
  887. rsav = ret;
  888. call(s->name, n->right, s->proc->left, s->proc->right, res);
  889. ret = rsav;
  890. }
  891. void
  892. ofmt(Node *n, Node *res)
  893. {
  894. expr(n->left, res);
  895. res->fmt = n->right->ival;
  896. }
  897. void
  898. owhat(Node *n, Node *res)
  899. {
  900. res->op = OCONST; /* Default return value */
  901. res->type = TLIST;
  902. res->l = 0;
  903. whatis(n->sym);
  904. }
  905. void (*expop[])(Node*, Node*) =
  906. {
  907. [ONAME] oname,
  908. [OCONST] oconst,
  909. [OMUL] omul,
  910. [ODIV] odiv,
  911. [OMOD] omod,
  912. [OADD] oadd,
  913. [OSUB] osub,
  914. [ORSH] orsh,
  915. [OLSH] olsh,
  916. [OLT] olt,
  917. [OGT] ogt,
  918. [OLEQ] oleq,
  919. [OGEQ] ogeq,
  920. [OEQ] oeq,
  921. [ONEQ] oeq,
  922. [OLAND] oland,
  923. [OXOR] oxor,
  924. [OLOR] olor,
  925. [OCAND] ocand,
  926. [OCOR] ocor,
  927. [OASGN] oasgn,
  928. [OINDM] oindm,
  929. [OEDEC] oeinc,
  930. [OEINC] oeinc,
  931. [OPINC] opinc,
  932. [OPDEC] opinc,
  933. [ONOT] onot,
  934. [OIF] 0,
  935. [ODO] 0,
  936. [OLIST] olist,
  937. [OCALL] ocall,
  938. [OCTRUCT] octruct,
  939. [OWHILE] 0,
  940. [OELSE] 0,
  941. [OHEAD] ohead,
  942. [OTAIL] otail,
  943. [OAPPEND] oappend,
  944. [ORET] 0,
  945. [OINDEX] oindex,
  946. [OINDC] oindc,
  947. [ODOT] odot,
  948. [OLOCAL] 0,
  949. [OFRAME] oframe,
  950. [OCOMPLEX] 0,
  951. [ODELETE] odelete,
  952. [OCAST] ocast,
  953. [OFMT] ofmt,
  954. [OEVAL] oeval,
  955. [OWHAT] owhat,
  956. };