Skip to content

[SUGGESTION] Treat a keyword as an identifier #392

Closed
@msadeqhe

Description

@msadeqhe

Preface

I suggest to use "keyword"$ syntax to treat keywords as identifiers in Cpp2.

Suggestion Detail

Some identifiers in Cpp2 are keywords in Cpp1 such as and, or, etc. These identifiers aren't valid identifiers in Cpp1. Therefore Cpp2 appends cpp2_ prefix to these identifiers during code generation for Cpp1 as discussed in this issue. For example identifiers and, or, etc in Cpp2 will respectively become identifiers cpp2_and, cpp2_or, etc in generated Cpp1 code.

On the other hand, some keywords in Cpp2 aren't keywords in Cpp1 such as type, next, etc. These keywords are valid identifiers in Cpp1. Therefore it's not possible to use such identifiers in Cpp2 when dealing with Cpp1 API:

// ERROR! `type` is a keyword, whereas it's an identifier in Cpp1.
v0: = type.name();

// ERROR! `next` is a keyword, whereas it's an identifier in Cpp1.
while next < 10 next next++ {
    //: statements...
}

I suggest to use a syntax such as "keyword"$ to access identifiers from Cpp1 API in which they are keywords in Cpp2:

// OK. `type` is an identifier.
v0: = "type"$.name();

// OK. `next` is an identifier.
while "next"$ < 10 next "next"$++ {
    //: statements...
}

I think syntax "keyword"$ is good enough because Cpp2 will have similar syntax for reflections and code generations as described in this page of Wiki. And semantically this is related to them.

Your Questions

Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code?

No.

Will your feature suggestion automate or eliminate X% of current C++ guidance literature?

No.

Considered Alternatives

I considered to use syntax @keyword but it would resemble meta-class functions. Another possible syntax is @"keyword", which doesn't resemble meta-class functions in addition to its difference from capture syntax.

Also we can use a prefix such as cpp1_. For example:

// OK. `cpp1_type` will become `type` in Cpp1.
v0: = cpp1_type.name();

// OK. `cpp1_next` will become `next` in Cpp1.
while cpp1_next < 10 next cpp1_next++ {
    //: statements...
}

In this way, cpp1_ prefix in addition to cpp2_ prefix should be reserved for Cpp2 compiler, and user-defined identifiers with those prefixes shouldn't be allowed.

By the way, treating a keyword as an identifier, isn't frequency needed to dedicate a new symbol to it.

EDIT 1: keyword<> is another alternative solution as described in this comment.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions