Skip to content

Commit

Permalink
Merge branch 'master' into new-parser
Browse files Browse the repository at this point in the history
Conflicts:
	library/Standard/Tuple
  • Loading branch information
stevefolta committed Mar 5, 2009
2 parents 1e3765a + c1b9f5f commit 9a82fc0
Show file tree
Hide file tree
Showing 26 changed files with 329 additions and 27 deletions.
11 changes: 11 additions & 0 deletions Notes
Expand Up @@ -9,6 +9,17 @@ Classes can see "my-field" (which is correct), but the codegen is a field access

To optimize field access, "MethodContext lookup-function:" would need to explicitly check fields before checking for function calls on "this". But that's not semantically safe, as a subclass could override the getter and/or setter.

=====

64-Bit
[2008.10.25]

Can't use setjmp()/longjmp() to pass the exception object, as they only pass an "int", not a "long". This is easily fixed by only passing 1 there, and using a global to hold the exception value. That's not thread-safe, but our exception chain isn't thread-safe currently either.

ClassInfo stores size in bytes, rt number of fields. That doesn't work when the generated C code is moved between 32 and 64 bit platforms.

Int should go to 64-bit on 64-bit platforms... but that probably won't be necessary to make it work.


=====

Expand Down
35 changes: 35 additions & 0 deletions docs/html/index.html
Expand Up @@ -83,6 +83,41 @@ <h2>

</div>

<div class="entry">
<div class="entry-title"> What does it look like? </div>
<p>
A simple "Hello World":
</p>

<div class="code">trylon hello-world

main: arguments
send: "Hello world!"
return 0</div>

<p>
A more object-oriented "Hello World":
</p>

<div class="code">trylon hello-world

class Hailer
field whom

create: whom
this whom = whom

hail
send: ("Hello, ", whom, "!")


main: arguments
hailer = Hailer new: "world"
hailer hail
return 0</div>

</div>

<div class="entry">
<div class="entry-title"> What is Trylon 2? </div>
<p>
Expand Down
32 changes: 32 additions & 0 deletions docs/source/index
Expand Up @@ -5,6 +5,38 @@ What is Trylon?

Trylon is a computer language. It is basically a cross between Python and Smalltalk. It uses indentation for <a href="code-structure.html">program structure</a>, like Python, and it uses Smalltalk's <a href="expressions.html">expression syntax</a> (but with precedence). Its <a href="objects.html">objects</a> are dynamically typed, but its programs are statically compiled (via C).

======

What does it look like?

A simple "Hello World":

trylon hello-world

main: arguments
send: "Hello world!"
return 0

A more object-oriented "Hello World":

trylon hello-world

class Hailer
field whom

create: whom
this whom = whom

hail
send: ("Hello, ", whom, "!")


main: arguments
hailer = Hailer new: "world"
hailer hail
return 0


======

What is Trylon 2?
Expand Down
54 changes: 54 additions & 0 deletions library/Readline/main
@@ -0,0 +1,54 @@
trylon Readline


c-preamble
#include <stdio.h>
#include <readline/readline.h>

c-fn readline: prompt
UsingClass_(MallocedString__Readline);
extern obj_ new_co___MallocedString__Readline(obj_, obj_);
char* cPrompt = (prompt ? CString_(prompt) : nil);
char* rawResult = readline(cPrompt);
if (rawResult == nil)
return nil;
return
new_co___MallocedString__Readline(
Proto_(MallocedString__Readline), (obj_) rawResult);


c-fn using-history
using_history();

c-fn stifle-history: size
stifle_history(IntValue_(size));
c-fn unstifle-history
unstifle_history();

c-fn add-history: line
return BuildInt_(add_history(CString_(line)));

c-fn read-history: path
return BuildInt_(read_history(CString_(path)));
c-fn write-history: path
return BuildInt_(write_history(CString_(path)));


class MallocedString
superclass String
field c-string

c-preamble
#include <string.h>
#include <stdlib.h>

c-fn create: c-string-in_
UsingMethod_(create_co_length_co_)
obj_ length = BuildInt_(strlen((const char*) c_string_in_ul_));
Field_(c_string) = c_string_in_ul_;
Call_(create_co_length_co_, this_, BuildBytePtr_(c_string_in_ul_), length);

c-fn destroy
free(Field_(c_string));


8 changes: 6 additions & 2 deletions library/Standard/CImplementation/File
Expand Up @@ -42,8 +42,10 @@ extend Standard File InStream
Field_(_dt_file) = (obj_) result;

c-fn close
if (Field_(_dt_file))
if (Field_(_dt_file)) {
fclose((FILE*) Field_(_dt_file));
Field_(_dt_file) = nil;
}

c-fn read-buffer: buffer length: length
return
Expand Down Expand Up @@ -73,8 +75,10 @@ extend Standard File OutStream
Field_(_dt_file) = (obj_) result;

c-fn close
if (Field_(_dt_file))
if (Field_(_dt_file)) {
fclose((FILE*) Field_(_dt_file));
Field_(_dt_file) = nil;
}

c-fn write-buffer: buffer length: length
return
Expand Down
13 changes: 13 additions & 0 deletions library/Standard/CImplementation/GarbageCollector
@@ -0,0 +1,13 @@
trylon GarbageCollector

extend Standard
class GarbageCollector
c-preamble
#include <gc/gc.h>

c-fn num-collections
return BuildInt_(GC_gc_no);

c-fn force-collection
GC_gcollect();

3 changes: 3 additions & 0 deletions library/Standard/CImplementation/Implementation
Expand Up @@ -8,6 +8,9 @@ extend Standard Implementation
c-fn allocate-bytes-non-ptr: num-bytes
return BuildBytePtr_(AllocNonPtr_(IntValue_(num_bytes)));

c-fn ptr-size
return BuildInt_(sizeof(obj_));

.target-language
return "C"

Expand Down
2 changes: 1 addition & 1 deletion library/Standard/CImplementation/Object
Expand Up @@ -30,7 +30,7 @@ extend Standard Object
return this_->class_->usedContexts;

c-fn .object-size
return BuildInt_(sizeof(classref_) + this_->class_->size);
return BuildInt_(sizeof(classref_) + this_->class_->numSlots * sizeof(obj_));

c-fn .added-fields
return this_->class_->addedFields;
Expand Down
1 change: 1 addition & 0 deletions library/Standard/CImplementation/main
Expand Up @@ -24,4 +24,5 @@ references
File
FileDirectory
Implementation
GarbageCollector

2 changes: 2 additions & 0 deletions library/Standard/Implementation/main
Expand Up @@ -4,6 +4,8 @@ allocate-object: proto with-extra-slots: num-extra-slots
allocate-bytes: num-bytes
allocate-bytes-non-ptr: num-bytes

ptr-size

.target-language


Expand Down
4 changes: 4 additions & 0 deletions library/Standard/JoltImplementation/main
Expand Up @@ -20,6 +20,10 @@ extend Standard Implementation
allocate-bytes-non-ptr: num-bytes
coke
(byte-ptr (GC_malloc_atomic [num-bytes _integerValue]))

ptr-size
coke
return '4

# This can't be set here for complicated reasons, but will be set elsewhere:
# .target-language = "Coke"
Expand Down
3 changes: 1 addition & 2 deletions library/Standard/Tuple
Expand Up @@ -3,8 +3,7 @@ trylon Tuple
field num-items

new: num-items
tuple = --
Implementation allocate-object: Tuple with-extra-slots: num-items
tuple = Implementation allocate-object: Tuple with-extra-slots: num-items
tuple num-items = num-items
return tuple

Expand Down
11 changes: 8 additions & 3 deletions library/Trylon_.c
Expand Up @@ -43,13 +43,16 @@ obj_ RespondsTo_(obj_ object, selector_ selector)

obj_ AllocObjFromClassInfo_(struct ClassInfo* classInfo)
{
obj_ object = (obj_) GC_MALLOC(sizeof(classref_) + classInfo->size);
obj_ object =
(obj_) GC_MALLOC(
sizeof(classref_) + classInfo->numSlots * sizeof(obj_));
object->class_ = classInfo;
return object;
}


static ExceptionCatcher_* currentExceptionCatcher = NULL;
obj_ currentException_ = NULL;

void PushException_(ExceptionCatcher_* catcher)
{
Expand All @@ -66,7 +69,8 @@ void Throw_(obj_ object)
exit(1);
}

longjmp(currentExceptionCatcher->jumpBuf, (int) object);
currentException_ = object;
longjmp(currentExceptionCatcher->jumpBuf, 1);
}


Expand Down Expand Up @@ -227,7 +231,8 @@ obj_ CloneObj_(obj_ object)
obj_ CloneObjExtra_(obj_ object, int numExtraFields)
{
size_t size =
sizeof(classref_) + object->class_->size + numExtraFields * sizeof(obj_);
sizeof(classref_) +
(object->class_->numSlots + numExtraFields) * sizeof(obj_);
obj_ newObject = (obj_) GC_MALLOC(size);
newObject->class_ = object->class_;
return newObject;
Expand Down
10 changes: 6 additions & 4 deletions library/Trylon_.h
Expand Up @@ -9,7 +9,7 @@ typedef struct ClassInfo* classref_;

struct ClassInfo {
int classNum;
int size;
int numSlots;
obj_ proto, parentContext, superclass;
obj_ usedContexts;
obj_ name;
Expand Down Expand Up @@ -77,6 +77,8 @@ extern obj_ AllocObjFromClassInfo_(struct ClassInfo* classInfo);
#define AllocObj_(className) \
(AllocObjFromClassInfo_(&className##__classInfo_))

extern void RegisterFinalizer_(obj_ object);


/* Standard objects */

Expand Down Expand Up @@ -241,19 +243,19 @@ typedef struct ExceptionCatcher_ {
extern void PushException_(ExceptionCatcher_* catcher);
extern void Throw_(obj_ object);
extern void PopException_();
extern obj_ currentException_;

#define Try_ \
{ \
ExceptionCatcher_ __catcher; \
obj_ exception; \
PushException_(&__catcher); \
exception = (obj_) setjmp(__catcher.jumpBuf); \
if (exception == nil) { \
if (setjmp(__catcher.jumpBuf) == 0) { \

#define TryElse_ \
PopException_(); \
} \
else { \
obj_ exception = currentException_; \
PopException_();

#define EndTry_ \
Expand Down
2 changes: 1 addition & 1 deletion sources/CCompiler/ClassBuilder
Expand Up @@ -85,7 +85,7 @@ emit-on: stream
stream write: "__classInfo_ = \n\t{ "
stream write: proto class-num string
stream write: ", "
stream write: (proto total-num-fields * 4) string
stream write: proto total-num-fields string
stream write: ", "
stream write-all: ("Proto_(", proto c-name, ")")
stream write: ", "
Expand Down
12 changes: 7 additions & 5 deletions sources/CCompiler/ExpandoArray
Expand Up @@ -7,8 +7,9 @@ create: block-size
this block-size = block-size
num-items = 0
num-blocks = 1
blocks = BytePtr new: 4 * num-blocks
(blocks ptr-at: 0) = BytePtr new: 4 * block-size
ptr-size = Implementation ptr-size
blocks = BytePtr new: ptr-size * num-blocks
(blocks ptr-at: 0) = BytePtr new: ptr-size * block-size


create
Expand Down Expand Up @@ -73,13 +74,14 @@ iterator

expand: new-num-blocks
# Copy the existing "blocks".
new-blocks = BytePtr new: 4 * new-num-blocks
new-blocks copy-from: blocks length: 4 * num-blocks
ptr-size = Implementation ptr-size
new-blocks = BytePtr new: ptr-size * new-num-blocks
new-blocks copy-from: blocks length: ptr-size * num-blocks

# Add the new blocks.
which-block = num-blocks
while which-block < new-num-blocks
(new-blocks ptr-at: which-block) = BytePtr new: 4 * block-size
(new-blocks ptr-at: which-block) = BytePtr new: ptr-size * block-size
which-block += 1

# Install the new "blocks".
Expand Down
4 changes: 4 additions & 0 deletions sources/CCompiler/Functions
Expand Up @@ -220,8 +220,10 @@ extend Compiler NewFunction
arg-names = List new
for arg in creator arguments
arg-names append: (mangle-name: arg name)

# Signature.
stream write-line: c-signature

# Body
proto-name = on-proto c-name
stream write: "{\n"
Expand All @@ -232,6 +234,8 @@ extend Compiler NewFunction
stream write: ", "
stream write: arg-name
stream write: ");\n"
if on-proto needs-finalizer
stream write: "\tRegisterFinalizer_(obj);\n"
stream write: "\treturn obj;\n"
stream write: "}\n\n\n"

Expand Down

0 comments on commit 9a82fc0

Please sign in to comment.