This commit is contained in:
Ingy döt Net 2013-04-10 16:19:29 -07:00
parent e5e8880e41
commit 518da4a923
1019 changed files with 15877 additions and 0 deletions

View file

@ -0,0 +1,4 @@
Show how to represent the boolean states "true" and "false" in a language. If other objects represent "true" or "false" in conditionals, note it.
;Cf.
* [[Logical operations]]

View file

@ -0,0 +1,2 @@
---
note: Basic language learning

View file

@ -0,0 +1,30 @@
BOOL f = FALSE, t = TRUE;
[]BOOL ft = (f, t);
STRING or = " or ";
FOR key TO UPB ft DO
BOOL val = ft[key];
UNION(VOID, INT) void = (val|666|EMPTY);
REF STRING ref = (val|HEAP STRING|NIL);
INT int = ABS val;
REAL real = ABS val;
COMPL compl = ABS val;
BITS bits = BIN ABS val; # or bitspack(val); #
BYTES bytes = bytes pack((val|"?"|null char)*bytes width);
CHAR char = (val|"?"|null char);
STRING string = (val|"?"|"");
print((((val | "TRUE" | "FALSE" ), ": ", val, or, (val|flip|flop), new line)));
print((" void: ", " => ", (void|(VOID):FALSE|TRUE), new line));
print((" ref: ", " => ", ref ISNT REF STRING(NIL), new line));
print((" int: ", int , " => ", int /= 0, new line));
print((" real: ", real , " => ", real /= 0, new line));
print((" compl: ", compl , " => ", compl /= 0, new line));
print((" bits: ", bits , " => ", ABS bits /= 0, or, bits /= 2r0, or,
bits width ELEM bits, or, []BOOL(bits)[bits width], new line));
print((" bytes: """, STRING(bytes) , """ => ", 1 ELEM bytes /= null char, or,
STRING(bytes) /= null char*bytes width, or,
STRING(bytes)[1] /= null char, new line));
print((" char: """, char , """ => ", ABS char /= 0 , or, char /= null char, new line));
print(("string: """, string , """ => ", string /= "", or, UPB string /= 0, new line));
print((new line))
OD

View file

@ -0,0 +1,18 @@
BEGIN {
# Do not put quotes round the numeric values, or the tests will fail
a = 1 # True
b = 0 # False
# Boolean evaluations
if (a) { print "first test a is true" } # This should print
if (b) { print "second test b is true" } # This should not print
if (!a) { print "third test a is false" } # This should not print
if (!b) { print "forth test b is false" } # This should print
# Boolean evaluation using comparison against zero
if (a == 0) { print "fifth test a is false" } # This should not print
if (b == 0) { print "sixth test b is false" } # This should print
if (a != 0) { print "seventh test a is true" } # This should print
if (b != 0) { print "eighth test b is true" } # This should not print
}

View file

@ -0,0 +1 @@
type Boolean is (False, True);

View file

@ -0,0 +1,11 @@
10 LET A%=0
20 LET B%=NOT(A%)
30 PRINT "THIS VERSION OF BASIC USES"
40 PRINT B%; " AS ITS TRUE VALUE"
50 IF A% THEN PRINT "TEST ONE DOES NOT PRINT"
60 IF B% THEN PRINT "TEST TWO DOES PRINT"
70 IF A%=0 THEN PRINT "TEST THREE (FALSE BY COMPARISON) DOES PRINT"
80 IF B%=0 THEN PRINT "TEST FOUR (FALSE BY COMPARISON) DOES NOT PRINT"
90 IF A%<>0 THEN PRINT "TEST FIVE (TRUE BY COMPARISON) DOES NOT PRINT"
100 IF B%<>0 THEN PRINT "TEST SIX (TRUE BY COMPARISON) DOES PRINT"
110 END

View file

@ -0,0 +1,7 @@
' BASIC256 used numbers to represent true and false
' values. Zero is false and anything else is true.
' The built in constants true and false exist
' and represent one and zero respectively.
print false
print true

View file

@ -0,0 +1,5 @@
REM BBC BASIC uses integers to represent Booleans; the keywords
REM FALSE and TRUE equate to 0 and -1 (&FFFFFFFF) respectively:
PRINT FALSE
PRINT TRUE

View file

@ -0,0 +1,28 @@
h1 = {foo: "bar"}
h2 = {foo: "bar"}
true_expressions = [
true
1
h1? # because h1 is defined above
not false
!false
[]
{}
1 + 1 == 2
1 == 1 # simple value equality
true or false
]
false_expressions = [
false
not true
undeclared_variable?
0
''
null
undefined
h1 == h2 # despite having same key/values
1 == "1" # different types
false and true
]

View file

@ -0,0 +1,6 @@
1> 1 < 2.
true
2> 1 < 1.
false
3> 0 == false.
false

View file

@ -0,0 +1,2 @@
TRUE . \ -1
FALSE . \ 0

View file

@ -0,0 +1 @@
data Bool = False | True deriving (Eq, Ord, Enum, Read, Show, Bounded)

View file

@ -0,0 +1,6 @@
if 0 then print "0" end -- This prints
if "" then print"empty string" end -- This prints
if {} then print"empty table" end -- This prints
if nil then print"this won't print" end
if true then print"true" end
if false then print"false" end -- This does not print

View file

@ -0,0 +1,35 @@
>> islogical(true)
ans =
1
>> islogical(false)
ans =
1
>> islogical(logical(1))
ans =
1
>> islogical(logical(0))
ans =
1
>> islogical(1)
ans =
0
>> islogical(0)
ans =
0

View file

@ -0,0 +1,2 @@
my $x = 0.0;
my $true_or_false = $x ? 'true' : 'false'; # false

View file

@ -0,0 +1,10 @@
my $x = 1; # true
my $true_or_false;
if ($x) {
$true_or_false = 'true';
}
else {
$true_or_false = 'false';
}

View file

@ -0,0 +1,5 @@
print (7 && 2); # 2, rather than 1(true)
print (2 && 7); # 7, rather than 1(true)
print (7 xor 2); # empty string, rather than 0(false)
print ('apples' && 'pears'); # pears, rather than 1(true)
print ('apples' xor 'pears'); # empty string, rather than 0(false)

View file

@ -0,0 +1,5 @@
# This does not work
# true and false are not special so will be treated as bareword strings
if (true) { print "true is true\n" }; # This prints
if (false) { print "false is true\n" }; # So does this
if (spongebob) { print "spongebob is true\n" }; # A bareword string

View file

@ -0,0 +1,39 @@
>>> True
True
>>> not True
False
>>> # As numbers
>>> False + 0
0
>>> True + 0
1
>>> False + 0j
0j
>>> True * 3.141
3.141
>>> # Numbers as booleans
>>> not 0
True
>>> not not 0
False
>>> not 1234
False
>>> bool(0.0)
False
>>> bool(0j)
False
>>> bool(1+2j)
True
>>> # Collections as booleans
>>> bool([])
False
>>> bool([None])
True
>>> 'I contain something' if (None,) else 'I am empty'
'I contain something'
>>> bool({})
False
>>> bool("")
False
>>> bool("False")
True

View file

@ -0,0 +1,2 @@
true = 1
false = 0

View file

@ -0,0 +1,2 @@
true = (1=1)
false = (1=0)

View file

@ -0,0 +1,2 @@
true = (1==1)
false = (1==0)

View file

@ -0,0 +1,2 @@
true = (1==1)
false = \true

View file

@ -0,0 +1 @@
false = ¬true

View file

@ -0,0 +1,4 @@
true = 1984 = 1984
false = 'war' = 'peace'
false = 'freedom' = 'slavery'
false = 'ignorance' = 'strength'

View file

@ -0,0 +1,3 @@
(cond ([(< 4 3) 'apple]
['bloggle 'pear]
[else 'nectarine])

View file

@ -0,0 +1,2 @@
$!/?\=false= + =true=#
\-/

View file

@ -0,0 +1,7 @@
v:BOOL := true; -- ok
i:INT := 1;
v := 1; -- wrong
if i then ... end; -- wrong: if requires a bool!
-- BUT
v := 1.bool; -- ok
if i.bool then ... end; -- ok

View file

@ -0,0 +1,2 @@
% if {""} then {puts true} else {puts false}
expected boolean value but got ""

View file

@ -0,0 +1 @@
if {[string is false -strict $string]} ...