Changed permission codes to only determine private/public/readonly

Changing permissions within the module was redundant and would have just
conflicted with the :mut type qualifier. This is easier to understand.
This commit is contained in:
Sasha Koshka 2022-08-18 12:09:17 -04:00
parent 15eb96e8ac
commit a548dcc585
5 changed files with 74 additions and 63 deletions

View File

@ -96,12 +96,11 @@ func (lexer *LexingOperation) tokenizeAlphaBeginning () (err error) {
token.location.SetWidth(len(got)) token.location.SetWidth(len(got))
if len(got) == 2 { if len(got) == 2 {
firstValid := got[0] == 'n' || got[0] == 'r' || got[0] == 'w' permission, isPermission := types.PermissionFrom(got)
secondValid := got[1] == 'n' || got[1] == 'r' || got[1] == 'w'
if isPermission {
if firstValid && secondValid {
token.kind = TokenKindPermission token.kind = TokenKindPermission
token.value = types.PermissionFrom(got) token.value = permission
} }
} }

View File

@ -126,10 +126,7 @@ func compareErr (
func TestTokenizeAll (test *testing.T) { func TestTokenizeAll (test *testing.T) {
checkTokenSlice("../tests/lexer/all.arf", test, checkTokenSlice("../tests/lexer/all.arf", test,
quickToken(3, TokenKindSeparator, nil), quickToken(3, TokenKindSeparator, nil),
quickToken(2, TokenKindPermission, types.Permission { quickToken(2, TokenKindPermission, types.PermissionReadWrite),
Internal: types.ModeRead,
External: types.ModeWrite,
}),
quickToken(2, TokenKindReturnDirection, nil), quickToken(2, TokenKindReturnDirection, nil),
quickToken(10, TokenKindInt, int64(-349820394)), quickToken(10, TokenKindInt, int64(-349820394)),
quickToken(9, TokenKindUInt, uint64(932748397)), quickToken(9, TokenKindUInt, uint64(932748397)),

View File

@ -78,9 +78,9 @@ func TestData (test *testing.T) {
checkTree ("../tests/parser/data", checkTree ("../tests/parser/data",
`:arf `:arf
--- ---
data wr integer:Int 3202 data ro integer:Int 3202
data wr integerArray16:{Int 16} data ro integerArray16:{Int 16}
data wr integerArrayInitialized:{Int 16} data ro integerArrayInitialized:{Int 16}
3948 3948
293 293
293049 293049
@ -92,18 +92,18 @@ data wr integerArrayInitialized:{Int 16}
0 0
4785 4785
92 92
data wr integerArrayVariable:{Int ..} data ro integerArrayVariable:{Int ..}
data wr integerPointer:{Int} data ro integerPointer:{Int}
data wr mutInteger:Int:mut 3202 data ro mutInteger:Int:mut 3202
data wr mutIntegerPointer:{Int}:mut data ro mutIntegerPointer:{Int}:mut
data wr nestedObject:Obj data ro nestedObject:Obj
.that .that
.bird2 123.8439 .bird2 123.8439
.bird3 9328.21348239 .bird3 9328.21348239
.this .this
.bird0 324 .bird0 324
.bird1 "hello world" .bird1 "hello world"
data wr object:Obj data ro object:Obj
.that 2139 .that 2139
.this 324 .this 324
`, test) `, test)

View File

@ -1,19 +1,19 @@
:arf :arf
--- ---
data wr integer:Int 3202 data ro integer:Int 3202
data wr mutInteger:Int:mut 3202 data ro mutInteger:Int:mut 3202
data wr integerPointer:{Int} data ro integerPointer:{Int}
data wr mutIntegerPointer:{Int}:mut data ro mutIntegerPointer:{Int}:mut
data wr integerArray16:{Int 16} data ro integerArray16:{Int 16}
data wr integerArrayVariable:{Int ..} data ro integerArrayVariable:{Int ..}
data wr integerArrayInitialized:{Int 16} data ro integerArrayInitialized:{Int 16}
3948 293 293049 948 912 3948 293 293049 948 912
340 0 2304 0 4785 92 340 0 2304 0 4785 92
@ -22,11 +22,11 @@ data wr integerArrayInitialized:{Int 16}
# data wr mutIntegerPointerInit:{Int}:mut [& integer] # data wr mutIntegerPointerInit:{Int}:mut [& integer]
data wr object:Obj data ro object:Obj
.this 324 .this 324
.that 2139 .that 2139
data wr nestedObject:Obj data ro nestedObject:Obj
.this .this
.bird0 324 .bird0 324
.bird1 "hello world" .bird1 "hello world"
@ -35,7 +35,7 @@ data wr nestedObject:Obj
.bird3 9328.21348239 .bird3 9328.21348239
# func rr main # func ro main
# --- # ---
# # TODO: set should be a special case, checking under itself for object # # TODO: set should be a special case, checking under itself for object
# member initialization args. it should also check for args in general # member initialization args. it should also check for args in general

View File

@ -1,48 +1,63 @@
package types package types
type Mode int type Permission int
const ( const (
ModeNone = iota // Displays as: pv
ModeRead //
ModeWrite // Other modules cannot access the section or member.
PermissionPrivate Permission = iota
// Displays as: ro
//
// Other modules can access the section or member, but can only read its
// value. It is effectively immutable.
//
// Data sections, member variables, etc: The value can be read by other
// modules but not altered by them
//
// Functions: The function can be called by other modules.
//
// Methods: The method can be called by other modules, but cannot be
// overriden by a type defined in another module inheriting from this
// method's reciever.
PermissionReadOnly
// Displays as: rw
//
// Other modules cannot only access the section or member's value, but
// can alter it. It is effectively mutable.
//
// Data sections, member variables, etc: The value can be read and
// altered at will by other modules.
//
// Functions: This permission cannot be applied to non-method functions.
//
// Methods: The method can be called by other modules, and overridden by
// types defined in other modules inheriting from the method's reciever.
PermissionReadWrite
) )
type Permission struct { // PermissionFrom creates a new permission value from the specified text. If the
Internal Mode // input text was not valid, the function returns false for worked. Otherwise,
External Mode // it returns true.
} func PermissionFrom (data string) (permission Permission, worked bool) {
worked = true
func ModeFrom (char rune) (mode Mode) { switch data {
switch (char) { case "pv": permission = PermissionPrivate
case 'n': mode = ModeNone case "ro": permission = PermissionReadOnly
case 'r': mode = ModeRead case "rw": permission = PermissionReadWrite
case 'w': mode = ModeWrite default: worked = false
} }
return
}
func PermissionFrom (data string) (permission Permission) {
if len(data) != 2 { return }
permission.Internal = ModeFrom(rune(data[0]))
permission.External = ModeFrom(rune(data[1]))
return
}
func (mode Mode) ToString () (output string) {
switch mode {
case ModeNone: output = "n"
case ModeRead: output = "r"
case ModeWrite: output = "w"
}
return return
} }
// ToString converts the permission value into a string.
func (permission Permission) ToString () (output string) { func (permission Permission) ToString () (output string) {
output += permission.Internal.ToString() switch permission {
output += permission.External.ToString() case PermissionPrivate: output = "pv"
case PermissionReadOnly: output = "ro"
case PermissionReadWrite: output = "rw"
}
return return
} }