twinbasic / lang-design

Language Design for twinBASIC
MIT License
11 stars 1 forks source link

Support string splicing and heredocs (multi-line strings) #19

Open retsyo opened 3 years ago

retsyo commented 3 years ago

currently, the BASIC's code dealing with string is tedious and hard to read. So what about some extended features to relieve the user

  1. support C-style escaped string, so that we can write and read code easily

    "To apply changes:\n C: disk\tSave your work"

    instead of

    "To apply changes:" & vbCrLf "C: disk" & vbTab & "Save your work"

    of cause, a new type of string should be introduced, for example Operator ! (Escaped String Literal) in freebasic

  2. string slice so we can write

    s[2: 5]

    instead of

    mid(s, 2, 2+5)

    this feature will save a lot of keyboard-typing and make the code shorter, especially a lot of LEFT/MID/RIGHT functions are need.

Further more, advanced operation can be performed easily, for example, a stride of 3 is used to get ph which is not easy to be done in BASIC

s = "python"
print(s[::3])
  1. multi-line string

    says "Multi-line string literals were introduced in Visual Basic 14 (in Visual Studio 2015)", but I don't know whether it supports escaped string and string interpolation
  2. string interpolation

    shows "string interpolation", which is not in VB6 according to what I remembered ```BASIC Dim primaryKey As String = "ID" Dim inventoryTable As String = "inventory"

Dim sql As String = $" SELECT {primaryKey}, Description FROM {inventoryTable} ORDER BY DateAdded "

Kr00l commented 3 years ago

Omg. Why? I believe it's good to be BASIC so people who work with VBA are immediately familiar with it and not introducing some hybrid monster which screws those people off. Just my two cents. Sorry.

XusinboyBekchanov commented 3 years ago

2015 UPDATE: Multi-line string literals were introduced in Visual Basic 14 (in Visual Studio 2015). The above example can be now written as:

Dim s As String = "Hello World & Space" MSDN article isn't updated yet (as of 2015-08-01), so check some answers below for details.

Details are added to the Roslyn New-Language-Features-in-VB-14 Github repository.

EduardoVB commented 3 years ago

Can a language have "add-ons" where you can add your custom parser logic? I think I never read of something like that, may be a new idea (not sure if good or bad).

bclothier commented 3 years ago

FWIW, I would love to see support for heredocs and string interpolation. Those two features usually improves the readability of the source code.

I don’t agree with using C style formatting. That violates the principle of having only one way of doing things. The string interpolation feature would still do the job without making it too different from BASIC.

For the slicing I have never used this feature. We already can cast a string into a byte array. I probably would hold off to see a demonstrable need for the feature that is easy to read and understand.

So in the end, I only want the heredoc and string interpolation.

vbRichClient commented 3 years ago

If there's some extensions on "normal Types" (later!) - then I'd prefer to use them with the (I think already agreed upon) "dot-notation" as (for example):

Same for SafeArrays:

As for "square bracketing" - I'd reserve that for a future "direct access on the String-allocation itself" - (avoiding the current fumbling with SafeArray-Mappings of a String to a 16Bit-IntegerType-Array):

That would be a usage of square-brackets, which would allow "convenient and fast implementations" (in all kind of scenarios, where such direct access provides a huge performance-boost, like e.g. Parsers or HashTable-implementations, whatever...)

Just my $0.02...

Olaf

FullValueRider commented 3 years ago

Just create a Library which implements the primitive types as objects. Similar to https://bitbucket.org/hwoarang84/vbtypes/wiki/Home but without the jiggery pokery behind the scenes. The overhead of creating objects should be much smaller with twinbasic.

mwolfe02 commented 3 years ago

I like the Python multi-line string syntax:

s = """
This is a
multi-line string
"""

If we use backticks for string interpolation (as @WaynePhillipsEA mentioned in another issue), then we could reuse the backticks for multi-line string interpolation:

s = ```
Dear {Recipient},

{Body}

Sincerely,
{Sender}
A9G-Data-Droid commented 3 years ago

@mwolfe02 When I see three quotes in a row it says to me that the string contains escaped quotes. That is the current VB behavior so we need to be careful not to confuse syntax here.

A9G-Data-Droid commented 3 years ago

In discussion #261 user @mansellan said:

Personally, I prefer $ - it's more canonical to BASIC:

* In VBx, it's used as a string hint,

* In VB.Net (and C#), it's used as an interpolation directive.

* In other BASICs it's the only identifier of a string.

It has history.

Backticks have never signified anything special in any BASIC dialog I can think of.

This is the example given in 4. of the OP in this thread. I think that moving towards VB.NET makes the most sense for this project. We can take the best parts of VB.NET and avoid the pitfalls making twinBASIC superior to all other BASICs.

mansellan commented 3 years ago

With interpolation, the need for c-style escaping is reduced, i.e:

$"Hello{vbNewLine}World"

(syntax TBD)

retsyo commented 3 years ago

c-style escaping keeps keyboard and wrist healthy

In my first issue post, I did not mention that python can use both single quotes and double quotes to make string, which is truly convenient during writing code to deal with strings/text. Why I did not mention that? Because this may let some nostalgic BASIC users uncomfortable. But to my mind,

  1. computer languages always evolve in some ways, one of which is to make user type/read code easily maybe via inventing some syntax or borrowing syntax from other languages.
  2. true nostalgic user should use QBasic for DOS, instead of VB/twinbasic/and any other modern BASIC implementations
EduardoVB commented 3 years ago

I like the Python multi-line string syntax:

s = """
This is a
multi-line string
"""

What About just one double quote at the first line and at the end?

s = "
This is a
multi-line string
"
EduardoVB commented 3 years ago

c-style escaping keeps keyboard and wrist healthy

In my first issue post, I did not mention that python can use both single quotes and double quotes to make string, which is truly convenient during writing code to deal with strings/text. Why I did not mention that? Because this may let some nostalgic BASIC users uncomfortable. But to my mind,

1. computer languages always evolve in some ways, one of which is to make user type/read code easily maybe via inventing some syntax or borrowing syntax from other languages.

2. true nostalgic user should use QBasic for DOS, instead of VB/twinbasic/and any other modern BASIC implementations

The problem that I see is that single quotes in VB mean starting a comment.

FullValueRider commented 2 years ago

As a starter for 10, I've uploaded a package containing the A CSharpish String.Format formatting helper code by Mathieu Guindon (aka RetailCoder).

My thanks to @WaynePhillipsEA in helping resolve some idiosyncrasies*.

*Absolutely and most definitely not crashes......