UNPKG

3.6 kBtext/coffeescriptView Raw
1# now this might be a little confusing, so a
2# clarification is in order.
3# First off, at the scripting level most things
4# as they are handled get evalled.
5# That means that they are recursively "calculated"
6# as much as possible, i.e. variables are recursively
7# looked up for their values, operators are applied,
8# functions are ivoked, etc.
9# I.e. while scripting, most things are
10# evalled all the times.
11# e.g. if I type
12# x = 1+1
13# then x is actually assigned 2, not 1+1
14# Something that helps a little is "quote", e.g.
15# If I assign
16# x = quote(1+1)
17# then x actually contains 1+1, not 2.
18# But then x is evaluated as soon as I type
19# x // gives "2" as x is evaluated
20#
21# Evaluation is great, but sometimes one wants
22# to look at the actual structure of an expression
23# or a content of a variable, without those
24# being evaluated first.
25#
26# for example I might type
27# x = a + b
28# a = 1
29# b = 2
30# and from this point on printing the actual
31# structure of x is impossible, because from
32# now on any evaluation of x will give "3"
33# You might say "but you have x defined up there,
34# what's the point of printing it out?", to which
35# the answer is that one might do further
36# substitutions or transformations of special kind
37# to x. One might want to look at the structure
38# and it might be complex or impossible.
39#
40# So this function does that.
41# If it's passed a variable, then it
42# DOES NOT eval the variable, RATHER
43# it prints the content of the variable without
44# evaluating it.
45# In the other cases it works like "quote" e.g.
46# it just gives the argument as is, again without
47# evaluating it.
48#
49# In the following examples, for brevity, I just
50# use
51# x = quote(1+2)
52# instead of this:
53# x = a + b
54# a = 1
55# b = 2
56# to put a structure in x that is easy to see whether
57# it's avaulated or not.
58#
59# So lookup allows this:
60# x = quote(1+2)
61# print(lookup(x)) # gives 1+2
62#
63# Note that there would be potentially a way
64# to achieve a similar result, you could do:
65# x = quote(quote(1+2))
66# print(x)
67# but you can't always control x to contain
68# two quotes like that...
69# note how two "quotes" are needed because
70# if you just put one, then
71# x would indeed contain 1+2 instead of 3,
72# but then print would evaluate that to 3:
73# x = quote(1+2) # now x contains 1+2, not 3
74# print(x) # but x evaluated here to 3
75#
76# Other workarounds would not work:
77# x = quote(1+2)
78# print(quote(x))
79# would not work because quote(x) literally means 'x'
80# so 'x' is printed instead of its content.
81#
82# Note also that lookup allows you to copy
83# the structure of a variable to another:
84# x = a + b
85# a = 1
86# b = 2
87# now:
88# y = x # y contains the number 3 and prints to 3
89# y = lookup(x) # y contains "a+b" and prints to 3
90# y = quote(x) # y contains "x" and prints to 3
91# note that in the first and second case y is
92# independent from x, i.e. changing x doesn't change y
93# while in the last case it is.
94#
95# Another similar simple example is when doing something
96# like this:
97# x = y
98# y = z
99# x
100# => gives z
101# lookup(x)
102# => gives y
103# i.e. lookup allows you to see the immediate
104# content of x, rather than the evaluation which
105# would end up in x -> y -> z
106# Note that if you invert the order of the assignments i.e.
107# y = z
108# x = y
109# Then at this point x immediately contains z, since the
110# assignment x = y is not quoted, hence y is evaluated to z
111# when assigned to x.
112# lookup(x)
113# => gives z
114
115Eval_lookup = ->
116 p1 = cadr(p1)
117 if !iscons(p1) and cadr(p1).k == SYM
118 p1 = get_binding(p1)
119 push p1
\No newline at end of file