PageRenderTime 650ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lisp/Control.cs

http://github.com/toshok/shelisp
C# | 121 lines | 98 code | 23 blank | 0 comment | 14 complexity | 4b8ffff4c22eda4ebc412dba2ed656c6 MD5 | raw file
Possible License(s): GPL-3.0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace Shelisp {
  5. public static class Control {
  6. [LispBuiltin (Unevalled = true)]
  7. public static Shelisp.Object Fprogn(L l, params Shelisp.Object[] forms)
  8. {
  9. Shelisp.Object rv = L.Qnil;
  10. for (int i = 0; i < forms.Length; i ++)
  11. rv = forms[i].Eval(l);
  12. return rv;
  13. }
  14. [LispBuiltin (Unevalled = true)]
  15. public static Shelisp.Object Fprog1(L l, Shelisp.Object form1, params Shelisp.Object[] forms)
  16. {
  17. Shelisp.Object rv = form1.Eval(l);
  18. for (int i = 0; i < forms.Length; i ++)
  19. forms[i].Eval(l);
  20. return rv;
  21. }
  22. [LispBuiltin (Unevalled = true)]
  23. public static Shelisp.Object Fprog2(L l, Shelisp.Object form1, Shelisp.Object form2, params Shelisp.Object[] forms)
  24. {
  25. Shelisp.Object rv;
  26. form1.Eval(l);
  27. rv = form2.Eval(l);
  28. for (int i = 0; i < forms.Length; i ++)
  29. forms[i].Eval(l);
  30. return rv;
  31. }
  32. [LispBuiltin (Unevalled = true)]
  33. public static Shelisp.Object Fif(L l, Shelisp.Object condition, Shelisp.Object then_form, params Shelisp.Object[] else_forms)
  34. {
  35. if (!L.NILP(condition.Eval(l))) {
  36. return then_form.Eval(l);
  37. }
  38. else {
  39. Shelisp.Object rv = L.Qnil;
  40. for (int i = 0; i < else_forms.Length; i ++)
  41. rv = else_forms[i].Eval(l);
  42. return rv;
  43. }
  44. }
  45. [LispBuiltin (Unevalled = true)]
  46. public static Shelisp.Object Fcond(L l, params Shelisp.Object[] clauses)
  47. {
  48. foreach (var clause in clauses) {
  49. Shelisp.Object condition = L.CAR(clause);
  50. Shelisp.Object rv = condition.Eval (l);
  51. if (!L.NILP(rv)) {
  52. Shelisp.Object body_forms = L.CDR(clause);
  53. if (!L.NILP (body_forms)) {
  54. foreach (var form in (List)body_forms)
  55. rv = form.Eval(l);
  56. }
  57. return rv;
  58. }
  59. }
  60. return L.Qnil;
  61. }
  62. [LispBuiltin]
  63. public static Shelisp.Object Fnot (L l, Shelisp.Object cond)
  64. {
  65. return L.NILP(cond) ? L.Qt : L.Qnil;
  66. }
  67. [LispBuiltin (Unevalled = true)]
  68. public static Shelisp.Object For (L l, params Shelisp.Object[] args)
  69. {
  70. for (int i = 0; i < args.Length; i ++) {
  71. Shelisp.Object evalled = args[i].Eval (l);
  72. if (!L.NILP (evalled))
  73. return evalled;
  74. }
  75. return L.Qnil;
  76. }
  77. [LispBuiltin (Unevalled = true)]
  78. public static Shelisp.Object Fand (L l, params Shelisp.Object[] args)
  79. {
  80. Shelisp.Object evalled = L.Qt;
  81. for (int i = 0; i < args.Length; i ++) {
  82. evalled = args[i].Eval (l);
  83. if (L.NILP (evalled))
  84. return evalled;
  85. }
  86. return evalled;
  87. }
  88. [LispBuiltin (MinArgs = 1, Unevalled = true)]
  89. public static Shelisp.Object Fwhile (L l, Shelisp.Object condition, params Shelisp.Object[] forms)
  90. {
  91. while (!L.NILP (condition.Eval (l))) {
  92. for (int i = 0; i < forms.Length; i ++)
  93. forms[i].Eval(l);
  94. }
  95. return L.Qnil;
  96. }
  97. }
  98. }