moo/stix/kernel/Console.st

207 lines
2.5 KiB
Smalltalk
Raw Normal View History

class Point(Object)
2016-12-31 17:22:57 +00:00
{
dcl x y.
2016-12-31 17:22:57 +00:00
method(#class) new
2016-12-31 17:22:57 +00:00
{
^self basicNew x: 0 y: 0.
}
method(#class) x: x y: y
2016-12-31 17:22:57 +00:00
{
^self basicNew x: x y: y.
}
method x
2016-12-31 17:22:57 +00:00
{
^self.x
}
method y
2016-12-31 17:22:57 +00:00
{
^self.y
}
method x: x
2016-12-31 17:22:57 +00:00
{
self.x := x
}
method y: y
2016-12-31 17:22:57 +00:00
{
self.y := y
}
method x: x y: y
2016-12-31 17:22:57 +00:00
{
self.x := x.
self.y := y
}
}
extend SmallInteger
2016-12-31 17:22:57 +00:00
{
method @ y
2016-12-31 17:22:57 +00:00
{
^Point x: self y: y
}
}
class Console(Object)
2016-12-31 17:22:57 +00:00
{
dcl handle.
2016-12-31 17:22:57 +00:00
"
method finalize
2016-12-31 17:22:57 +00:00
{
handle notNil ifTrue: [
self _close: handle.
]
}
"
## method(#class) input
2016-12-31 17:22:57 +00:00
## {
## ^self new _open: filename mode: mode
## }
method(#class) output
2016-12-31 17:22:57 +00:00
{
| c |
c := self new.
c handle: (c _open).
^c
}
## method(#class) error
2016-12-31 17:22:57 +00:00
## {
## }
method handle: v
2016-12-31 17:22:57 +00:00
{
self.handle := v.
}
method close
2016-12-31 17:22:57 +00:00
{
self _close: self.handle.
self.handle := nil.
}
method write: text
2016-12-31 17:22:57 +00:00
{
^self _writeOn: self.handle text: text.
}
method clear
2016-12-31 17:22:57 +00:00
{
^self _clear: self.handle
}
method setCursor: point
2016-12-31 17:22:57 +00:00
{
^self _setCursor: self.handle point: point.
}
"
method _open: filename mode: mode
2016-12-31 17:22:57 +00:00
{
self.handle := self __open: filename mode: mode.
^self.
}
method __open: filename mode: mode
2016-12-31 17:22:57 +00:00
{
<primitive: #console.open>
##StdioException signal: ('cannot open ' & filename).
}
"
method _open
2016-12-31 17:22:57 +00:00
{
<primitive: #console.open>
}
method _close: handle
2016-12-31 17:22:57 +00:00
{
<primitive: #console.close>
self primitiveFailed.
}
method _clear: handle
2016-12-31 17:22:57 +00:00
{
<primitive: #console.clear>
self primitiveFailed.
}
method _writeOn: handle text: text
2016-12-31 17:22:57 +00:00
{
<primitive: #console.write>
self primitiveFailed.
}
method _setCursor: handle point: point
2016-12-31 17:22:57 +00:00
{
<primitive: #console.setcursor>
self primitiveFailed.
}
"
method(#class) open
2016-12-31 17:22:57 +00:00
{
<primitive: #console.open>
self primitiveFailed.
}
method close
2016-12-31 17:22:57 +00:00
{
<primitive: #console.close>
self primitiveFailed.
}
method setCursorTo: point
2016-12-31 17:22:57 +00:00
{
<primitive: #console.setcursor>
self primitiveFailed.
}
"
##x := Colsole new.
##x := Console open.
##(x isError) ifTrue: [
## handle error...
## ]
## ifFalse: [
## x setCursor (1, 2).
## x clear.
## x close.
## ]
##x := File open: 'abc.def'
##t := x read: 1000.
##x close.
}
"
Stix define: 'console_write'
forClass: Console
method: 'write: aString upto: length'
returns: 'size_t'
arguments: 'void* size_t'
---> produces a method like this internally...
class Console
2016-12-31 17:22:57 +00:00
{
method write: aString upto: length
2016-12-31 17:22:57 +00:00
{
<ffi: int console_write (int*, char*, [int, int, char]* )> <== parse the string, create a descriptor table, key is console_write, value is resolved to a function pointer.
}
}
"