File: prim.c

package info (click to toggle)
marlais 0.6.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,192 kB
  • ctags: 2,068
  • sloc: ansic: 16,958; yacc: 1,150; lex: 870; makefile: 639; lisp: 248
file content (155 lines) | stat: -rw-r--r-- 4,386 bytes parent folder | download
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
/*

   prim.c

   This software is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This software 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this software; if not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   Original copyright notice follows:

   Copyright, 1993, Brent Benson.  All Rights Reserved.
   0.4 & 0.5 Revisions Copyright 1994, Joseph N. Wilson.  All Rights Reserved.

   Permission to use, copy, and modify this software and its
   documentation is hereby granted only under the following terms and
   conditions.  Both the above copyright notice and this permission
   notice must appear in all copies of the software, derivative works
   or modified version, and both notices must appear in supporting
   documentation.  Users of this software agree to the terms and
   conditions set forth in this notice.

 */

#include <string.h>

#include "prim.h"

#include "alloc.h"
#include "bytestring.h"
#include "error.h"
#include "list.h"
#include "symbol.h"

void
init_prims (int num, struct primitive prims[])
{
  int i;
  Object sym, prim;

  for (i = 0; i < num; ++i) {
    sym = make_symbol (prims[i].name);
    prim = make_primitive (prims[i].name, prims[i].prim_type, prims[i].fun);
    /* just for now, don't add %foo to be exported */
    add_top_lvl_binding1(sym, prim, 0, prims[i].name[0] != '%');
  }
}

Object
make_primitive (char *name, enum primtype type, Object (*fun) ())
{
  Object obj;

#ifndef SMALL_OBJECTS
  obj = allocate_object (sizeof (struct primitive));
#else
  obj = allocate_object (sizeof (struct prim));
#endif

  PRIMTYPE (obj) = Primitive;
  PRIMNAME (obj) = checking_strdup (name);
  PRIMPTYPE (obj) = type;
  PRIMFUN (obj) = fun;
  return (obj);
}

Object
apply_prim (Object prim, Object args)
{
#if defined(MACOS) && defined(THINK_C)
  Object (*fun) (...);
#else
  Object (*fun) ();
#endif

  fun = PRIMFUN (prim);
  switch (PRIMPTYPE (prim)) {
  case prim_0:
    return (*fun) ();
  case prim_1:
    return (*fun) (FIRST (args));
  case prim_2:
    return (*fun) (FIRST (args), SECOND (args));
  case prim_3:
    return (*fun) (FIRST (args), SECOND (args), THIRD (args));
  case prim_0_1:
    switch (list_length (args)) {
    case 0:
      return (*fun) (NULL);
    case 1:
      return (*fun) (FIRST (args));
    default:
      error ("incorrect number of args to primitive", prim, NULL);
    }
  case prim_0_2:
    switch (list_length (args)) {
    case 0:
      return (*fun) (NULL, NULL);
    case 1:
      return (*fun) (FIRST (args), NULL);
    case 2:
      return (*fun) (FIRST (args), SECOND (args));
    default:
      error ("incorrect number of args to primitive", prim, NULL);
    }
  case prim_0_3:
    switch (list_length (args)) {
    case 0:
      return (*fun) (NULL, NULL, NULL);
    case 1:
      return (*fun) (FIRST (args), NULL, NULL);
    case 2:
      return (*fun) (FIRST (args), SECOND (args), NULL);
    case 3:
      return (*fun) (FIRST (args), SECOND (args), THIRD (args));
    default:
      error ("incorrect number of args to primitive", prim, NULL);
    }
  case prim_1_1:
    switch (list_length (args)) {
    case 1:
      return (*fun) (FIRST (args), NULL);
    case 2:
      return (*fun) (FIRST (args), SECOND (args));
    default:
      error ("incorrect number of args to primitive", prim, NULL);
    }
  case prim_2_1:
    switch (list_length (args)) {
    case 2:
      return (*fun) (FIRST (args), SECOND (args), NULL);
    case 3:
      return (*fun) (FIRST (args), SECOND (args), THIRD (args));
    default:
      return error ("incorrect number of args to primitive", prim, 0);
    }
  case prim_0_rest:
    return (*fun) (args);
  case prim_1_rest:
    return (*fun) (CAR (args), CDR (args));
  case prim_2_rest:
    return (*fun) (FIRST (args), SECOND (args), CDR (CDR (args)));
  default:
    return error ("cannot handle primitive type", prim, NULL);
  }
}