SyntaxBomb - Indie Coders

General Category => General Discussion => Topic started by: Xaron on June 24, 2018, 19:19:35

Title: Random question about languages
Post by: Xaron on June 24, 2018, 19:19:35
In this thread I'd like to ask some questions where they just pop up in my mind. I just stumbled across things because I currently write a tokenizer/parser and just wonder about things while doing this.

So here's the first question: Why do languages like BlitzBasic, Monkey, even AGK need the keyword "Local" to declare a locale variable?

Why not just:
i:Int = 1

instead of:
Local i:Int = 1

Is there a deeper reason for this? Same is true for the Global keyword even though one could declare a global within a scope even though I personally think this is a bad design decision. What do you think?

Second question: What do you like as a starting character for comments?
;
'
//

? Does it even matter?
Title: Re: Random question about languages
Post by: GW on June 24, 2018, 20:02:02
Without the word local, it's an assignment, not a declaration. Or at least ambiguous to a parser.  Local also defines the scope of a variable. It helps compiler optimize things and free's the programmer having to hold a large mental model of the total scope.   
Title: Re: Random question about languages
Post by: Henri on June 24, 2018, 20:46:45
Hi,

local also means that variable resides in dynamic  memory (we need resources, allocate them, use them and then discard and left to be collected)
and global means static (c-keyword) that is accessible everywhere, and has reserved memory that stays through program lifecycle.

without this distinction would every variable be dynamic ? As far as I know, every language has these declarations (I could be wrong but anywho -:)).

-Henri
Title: Re: Random question about languages
Post by: Steve Elliott on June 24, 2018, 21:13:12
Quote
So here's the first question: Why do languages like BlitzBasic, Monkey, even AGK need the keyword "Local" to declare a locale variable?

Partly why I don't use those languages - unnecessarily verbose.  In C++ and my own language scope is obvious without adding extra words.
Title: Re: Random question about languages
Post by: Derron on June 24, 2018, 22:53:48
Code (BlitzMax) Select

Type TTest
  Field x:int

  Method superduper()
    local x:int = 10
    self.x = 20
    print x + "  " + self.x
  End Method
End Type

new TTest.superduper()


Without the "local" you would see "20 20" instead of "10 20" printed.


bye
Ron
Title: Re: Random question about languages
Post by: col on June 25, 2018, 00:03:13
I think the point Xaron is trying to get across is something like

'Why not use this'

Type TTest
  x:int

  Method superduper()
    x:int = 10
    self.x = 20
    print x + "  " + self.x
  End Method
End Type

new TTest.superduper()


Personally I agree that the Global, Field and Local keywords are unnecessary, and Global should be replaced with 'Static'.

QuoteSecond question: What do you like as a starting character for comments?
I use ' for a complete line comment and

'/ comment /'

for multi-line and inline comments.
Title: Re: Random question about languages
Post by: Steve Elliott on June 25, 2018, 00:08:10
Quote
Personally I agree that the Global, Field and Local keywords are unnecessary.

Definitely.
Title: Re: Random question about languages
Post by: Derron on June 25, 2018, 07:11:41
Quote from: col on June 25, 2018, 00:03:13
Personally I agree that the Global, Field and Local keywords are unnecessary, and Global should be replaced with 'Static'.

In German "statisch" is the translation for "static". One of the synonyms of "statisch" is "konstant" (English: "constant"). It means that something is not moving, does not change, fixed, invariable - but also "is there forever". Nonetheless the main connotation is "fixed/not changing".

"Global" on the other hand just translates to the German "global" as well, meaning "everywhere around the world", "worldwide", ... which pretty much is the same as "everywhere in the code".


So for me - as a German speaking coder - "global" really suits better than "static".
In Java "static" is some kind of "class-wide variable" - so what about "program wide variables"?


@ comments
I prefer /* ... */ if possible, but the apostrophe ' is also good to go ...
But there could also be the tertiary question if you put a whitespace after the comment token?

' hi
'hi
//commented out
// commented out

I tend to comment out code by steering the caret to the start of the line's actual content and just prepend the comment token - so no whitespace between "code" and "//". This of course most often is done that way as I use "ctrl + left/right" to navigate in lines and that "Geany" knows where you move along (move from "begin to begin"), so cursor up/down does not strictly move the caret from line 3:20 to line 4:20 but to the equal part of the above/below line.


bye
Ron
Title: Re: Random question about languages
Post by: col on June 25, 2018, 09:38:14
QuoteIn German "statisch" is the translation for "static". One of the synonyms of "statisch" is "konstant"
Interesting.

QuoteWhy not just:
i:Int = 1

In many other languages the 'type' of the variable comes first. I think this makes sense when it comes to declaration/definition of a variable and is less verbose than 'basic' languages that have the 'type' after the variable.

'type' after the variable...
Local a:Int = 1, b:Int = 2, c:Int = 4, d:Float = 10.0

'type' before the variable without Local...
Int a=1, b=2, c=3, Float d = 10.0

While on this subject - what would people prefer to denote type for constant numerals...
10.f    ' float value
10.0f  ' float value
10.d   ' double value
10.0   ' double value
1010110b ' binary
$123ABC   ' hex
0x123ABC ' hex
0b110110 ' binary

??
I'm sure there are more options too, which syntax do you prefer?

QuoteBut there could also be the tertiary question if you put a whitespace after the comment token?
Could you elaborate with what you mean here.


Title: Re: Random question about languages
Post by: Xaron on June 25, 2018, 09:44:29
Thanks guys, very helpful insights! :)

@col: Yes, that's exactly what I meant.
Title: Re: Random question about languages
Post by: Xaron on June 25, 2018, 09:46:26
Quote from: col on June 25, 2018, 09:38:14
In many other languages the 'type' of the variable comes first. I think this makes sense when it comes to declaration/definition of a variable and is less verbose than 'basic' languages that have the 'type' after the variable.

Ah haven't thought about that. Yes, in that case it makes sense to declare it with 'Local' before. It's more error prove then I guess.

So that means that for languages like Basic which have the type after the variable name it makes sense to have a 'Local' before the name to make it more clear.
Title: Re: Random question about languages
Post by: Derron on June 25, 2018, 10:38:13
@ (white)space before comment token
as written:
Code (BlitzMax) Select

' hey whitespace
'  even more whitespace
'no whitespace


BTW: {code=BlitzMax}...{/code} if you prefer semi-proper syntax highlighting.


bye
Ron
Title: Re: Random question about languages
Post by: col on June 25, 2018, 10:59:21
I still don't understand what you mean  :P
Those comment lines are all the same thing to a parser. That is once the parser sees the 'line comment token' ( the apostrophe - ascii character 39 ) it will classify everything after it as a comment until either end-of-line or end-of-file, whichever is reached first.

Thanks for the 'code=BlitzMax' tip :)

edit: Correct if I'm wrong in that are you suggesting different things depending on if there is whitespace between the 'line comment token' and the 'commented text'?
Title: Re: Random question about languages
Post by: Derron on June 25, 2018, 12:33:06
No I just wanted to know what you prefer.
Eg. Brucey tends to use a whitespace between comment token and comment value - others too.

I most often skip adding any whitespace (as described above). But hey, I also set my Geany to remove whitespace at the end of lines (can look scary if you edit someone elses code files .... and the DIFF consists of 90 changed lines while you only appended a single line of code -- you then need to disable the auto-trim).


bye
Ron
Title: Re: Random question about languages
Post by: col on June 25, 2018, 15:19:19
Ahh I see.
I prefer a space between them.

@Xaron
What are you going to work on?
Title: Re: Random question about languages
Post by: Xaron on June 25, 2018, 15:38:52
@col BB3D2  ;)
Title: Re: Random question about languages
Post by: Phil7 on June 25, 2018, 15:44:59
A space between ' and the comment value makes also sense for easier reading. An adult reader seems to recognise a word by looking only at the first letter and some other hints, so anything disturbing there could decrease the speed of perception.

Regarding 'local' I would also prefer to write only variableName:int=10 and an error message if :int is occuring in another place.
Could anyone give an example were this could lead to an ambiguity (using monkey/cerberus)?
Title: Re: Random question about languages
Post by: col on June 25, 2018, 16:03:35
Nice :)

Any details on features?
Title: Re: Random question about languages
Post by: col on June 25, 2018, 21:30:46
QuoteCould anyone give an example were this could lead to an ambiguity
I've used this approach with a BlitzMax style variant and it works well. I used the colon to be followed by a valid type to mean a variable declaration with an optional initialiser expression.
Title: Re: Random question about languages
Post by: Kryzon on June 26, 2018, 03:13:43
Quote from: Derron on June 24, 2018, 22:53:48
Code (BlitzMax) Select

Type TTest
  Field x:int

  Method superduper()
    local x:int = 10
    self.x = 20
    print x + "  " + self.x
  End Method
End Type

new TTest.superduper()
To me the 'self.x' is what differentiates between the member x and the local x. So the 'local' qualifier is not needed (I know that's how BlitzMax behaves, but it's logically redundant).
self.x = member x
x = x that's not a member of the class.

In Python if you forget the 'self.' suffix you will start creating local variables, even if they have the same name as members.
Title: Re: Random question about languages
Post by: Xaron on June 26, 2018, 08:19:39
Quote from: col on June 25, 2018, 16:03:35
Nice :)

Any details on features?

Hey col, the plan is as follows:

We (Mike and me) want to create a Basic Language which is easy to use like BB3D.
There will be an interpreter for debugging purposes and fast development plus easy debugging and a transpiler which translates everything to C++ for maximum performance.
There will be the possibility to use C++ together with the libs at all like Tier 2 for AGK.

For the first version we aim for Windows only which already is quite a task. It shall include steam integration and all that stuff plus 2d/3d, sound, physics and networking. If that's done, I'd love to add more targets like Mac, HTML5, Android and iOS but that's very future, so we focus on Windows first.

For the 2d/3d part we still think about what to use as backend. For know I currently check out Irrlicht + IrrlichtBAW, Ogre3d, Urho3d and bgfx. The plan is to use a modern renderer pipeline. Maybe it's an option to use SDL2 for 2d and other stuff for 3d.

At the moment I'm in the process of writing the lexer and parser (C++) and creating the interpreter for just the language itself so it's still a way to go but it's pure fun. :)

The motivation

Ok, some words about why I am (we are) doing this. I grew up with a C64 and Basic was the first language I got in contact with. Shortly after (I was 12 at that point) I started with assembly language and used it for many years, even following by different Amiga computers. I LOVED that 68000 motorola CPU, the Intel compared to that was a nightmare when it comes to assembly language. I even did the GUI stuff and all that in assembly, that was quite funny.

Then I came to PC and somehow stumbled across BlitzBasic and WOW I was in love. Used it for many years, but we all know how the story did end... :/

So I already had the plan to create something by myself a few years ago. But I never really did because there always were good alternatives. Monkey came out and I switched over there, created some ( 8 ) games using it for mobiles and was quite happy using it. Then it died... Monkey 2 came out but for me personally I never got warm with it due to the complexity. Sure, it's a nice language but using C++ day in day out for about two decades now I simply doesn't see the need for another fancy language to learn. So I went to AGK and had the same warm feeling as with BB3D in the beginning. But somehow it just doesn't feel the same. I think it has way too many commands, some things are strange done and unnecessary complex. I even tried out Unity and did a pretty successful game with it but... oh boy, I just hate that tool. So here I am. There is nothing out there atm which creates the same good warm feeling like BB3D did in the past and I want to try to revive that. :)
Title: Re: Random question about languages
Post by: col on June 26, 2018, 10:25:56
@Xaron
Sounds interesting. Happy coding ahead :)

Are you changing the original language syntax ( therefore writing a new lexer and parser ) ?
The only reason I ask is because the source is in the public domain on BRLs github and if you ARE keeping things the same then why reinvent the wheel? :P
Title: Re: Random question about languages
Post by: Xaron on June 26, 2018, 11:31:28
I do it from scratch, yes. Simply because I like some C++11 features.  ;D

And that makes it much easier for me to fix things when I did it myself.

BTW on topic: I would just not add the keywords goto and gosub because... well. I know, they are part of the Basic language but I don't think they are needed at all, are they?
Title: Re: Random question about languages
Post by: TomToad on June 26, 2018, 13:10:23
When I was using BlitzMax,I decided to start using Superstrict on all my programs. I was hesitant at first, because it meant losing goto and gosub. After using Superstrict for a while,I found I didn't miss those commands at all. Don't see any use for them at all in a well structured language.
Title: Re: Random question about languages
Post by: Xaron on June 26, 2018, 13:21:52
I think settings like strict or even superstrict should be enabled by default.
Title: Re: Random question about languages
Post by: meems on June 26, 2018, 21:32:04
@tomtoad my understanding is that goto and gosubs are useful when pushing algorithms to their speed limit. they are faster than function and method calls. These days for most coding, speed isn't so crucial as to prefer gosubs over functions. perhaps a compiler can intepret zero parameter functions as gosubs for a minor speed increase.
Title: Re: Random question about languages
Post by: Xaron on June 27, 2018, 06:26:16
Quote from: meems on June 26, 2018, 21:32:04
@tomtoad my understanding is that goto and gosubs are useful when pushing algorithms to their speed limit. they are faster than function and method calls. These days for most coding, speed isn't so crucial as to prefer gosubs over functions. perhaps a compiler can intepret zero parameter functions as gosubs for a minor speed increase.

That was true ages ago but nowadays it's really not an issue anymore, especially with compiler optimization levels beyond our imagination. ;)
Title: Re: Random question about languages
Post by: col on June 27, 2018, 10:02:22
My personal 2 pennies...

For BASIC variants I wouldn't entertain versions of 'Strict' - it just creates headaches if you want to expand the language. I'd either have explicit type declaration of a variable, or you don't and you default 'no type declaration' as an integer.
I'd keep the language as strongly typed as possible. I think type inference is just lazy and starts heading towards hidden ambiguities that are silently resolved incorrectly.
The same with type conversion - it should be explicit for clarity: 'Numbers -> string' is ok to be implicitly converted, and 'int -> float/double', 'float/double -> int' should be ok with a warning of possible loss of data, but all other types, including 'string -> number' should be explicitly converted.

I agree with your thoughts on Goto and Gosub.
Whilst they are very powerful ( assembler doesn't have much more than these for moving around inside executable code ) they don't really belong in higher level languages anymore due to compiler optimisations.

For BB3D2 I'd...
Fix the array syntax.
Allow types to have methods for OO but have composition as opposed to inheritance - kind of how B3D is now ( except B3D doesn't support methods of course ).
Fix the Type system to work the same as BlitzMax except there would be no default base 'Object'.
Introduce 'collections' ( structured containers ) which would have a specific type for the instances it holds.
Title: Re: Random question about languages
Post by: Xaron on June 27, 2018, 10:06:30
Thanks col, yeah inheritance is probably one of the most misused and misunderstood concepts in OO languages. It's often just evil. In our company I sometimes see hierarchies of 10 derived classes. o_O
Title: Re: Random question about languages
Post by: col on June 27, 2018, 10:29:11
Quoteperhaps a compiler can intepret zero parameter functions as gosubs for a minor speed increase.
The major compilers ( Intels icc, GNUs gcc, MSVCs cl ) are really very advanced to yesterday and all are capable of inlining functions regardless of the parameters. Release builds can also be very aggressive when it comes to optimising. Trying to debug the assembly of a release build takes more effort due to optimisations - a lot of variables/parameters can stay in registers across many function boundaries, especially if the function was inlined - code is also moved around and executed 'out of order' to how the code was originally written as long as the result is the same as if it was executed 'in order'.
Title: Re: Random question about languages
Post by: col on June 27, 2018, 20:03:26
QuoteIn our company I sometimes see hierarchies of 10 derived classes.
Thats just crazy  :o
omg  :P
Title: Re: Random question about languages
Post by: Henri on June 27, 2018, 20:42:04
@Local

Comrate GW had a valid point though: How would you distinguish between declaration and assignment ? In C assignment is recognized with = (edit. whoops, got assignments and comparisons mixed up. You see already confusing :-)  . Ambiguity is the number one cause of bugs in this world.


@Inheritance
I remember reading somewhere that usually, if you find yourself inheriting more than 2 times, you are probably doing it wrong.


-Henri
Title: Re: Random question about languages
Post by: Steve Elliott on June 27, 2018, 21:52:36
10 derived classes! PMSL.  :o
Title: Re: Random question about languages
Post by: col on June 27, 2018, 21:53:04
QuoteHow would you distinguish between declaration and assignment ? In C assignment is recognized with ==
In C assignment is recognised with = and equality ( compare equal ) is recognised with ==.

C/C++ are different from what most folks that are used to BASIC languages are used to in that you can declare a variable ( which gives the compiler a brief description of a variable or function ) and you can also define it separately. You can also declare and define at the same time to. When you declare a 'thing' ( variable, function, function pointer, struct, class etc) you would just give the type and name of it. When you define that 'thing' you fill in all of the missing pieces ( such as the meat and guts of a function or class ). You can't use a 'thing' until it is also has a  definition somewhere - Even more confusing is that the code will probably compile, at least up to the point of linking which is when the compiler looks for the definition. If you encounter the 'undefined reference to blah::blah()' then you're missing the definition, ie the actual function itself ( which would usually, but not always, be in a lib file somewhere ) when you link with static libraries.

Oops I kinda wondered off there, and there is also much more to say about it, and with examples, but I don't mean to go off on a pointless tangent :p

For a BASIC-like language you *could* have it that when you define a variable and define its type along with that variable then that is a new definition, no need for Local etc.
It's also up to the language designer if they require you to put the type before of after the variable name.

So for a new variable you would have

Function something()
    x:Int = 10
    x:Int            ' compiler error... duplicate definition
    x = 20          ' assignment
EndFunction
Title: Re: Random question about languages
Post by: Xaron on June 28, 2018, 08:25:15
Haha yes guys I know... That inheritance has come due to "historical reasons". Our codebase is huge.

So would something like


Function something()
    x:Int = 10
    x:Int            ' compiler error... duplicate definition
    x = 20          ' assignment
EndFunction


really ok? Or is it somehow confusing for someone?

Is it "easier" to have:


Function something()
    Local x:Int = 10
    Local x:Int    ' compiler error... duplicate definition
    x = 20          ' assignment
EndFunction
Title: Re: Random question about languages
Post by: MikeHart on June 28, 2018, 08:40:51
I would rather see it like this:
Function something()   
  Int x = 10   
  '....   
  x = 20
EndFunction

But then, this isn't BASIC style.
Title: Re: Random question about languages
Post by: Xaron on June 28, 2018, 09:10:51
Indeed, I somehow would prefer it that way as well as I think it has better readability... But yes, it's not classic Basic style. Question is, does it have to?
Title: Re: Random question about languages
Post by: col on June 28, 2018, 10:57:28
@Henri,
I know what you mean. The amount of times I've discovered a bug due to missing the  '=' key for the second one in '==' and ending up with just '=' is ridiculous. C is bordering 'bearable' but C++ with its STL has turned into a hideous ugly language. I posted a little while ago about how I had a syntax error in 1 place which threw up over 500 errors :D
Title: Re: Random question about languages
Post by: Xaron on June 28, 2018, 11:52:21
Those template error messages can be "fun".  ;D