Skip to content

Commit

Permalink
comments / doc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cvs0 committed Apr 19, 2024
1 parent 2231282 commit ea343e5
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 35 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ In this code, we declare two variables, `x` and `y`, and use the addition operat
These are just basic examples of CVSCode's functionality. You can explore more advanced features, data structures, and control flow as you delve deeper into your CVSCode scripts.
## Native Functions
cvsCode has many built-in functions, the list is [here](/docs/natives.md)
# Features
CVSCode offers a wide range of features, including:
Expand Down Expand Up @@ -142,24 +145,24 @@ This project is made possible by the contributions and efforts of the following
Note: All of these issues are planned to be fixed. This section is for making them public knowledge for contributors and active users.
- The `run` file command only works inside the VSCode integrated terminal.
- ~~The `run` file command only works inside the VSCode integrated terminal.~~
- Variable names cannot contain integers.
- Some operators do not work as of now.
- ~~Some operators do not work as of now.~~
## Pull Request Requirements
### TypeScript and Deno Specific Guidelines
To ensure consistency and maintain the quality of the CVSCode project, we have established the following requirements for pull requests specifically related to TypeScript and Deno:
1. **Code Conformance**
2. **Linting**
3. **Testing**
4. **Documentation**
5. **Dependencies**
6. **Commit Messages**
7. **Branching Strategy**
8. **Review and Discussion**
1. **Code Conformance** | Your code must fit with the style of the rest of our code to create a clean codebase for anyone who wants to commit.
2. **Linting** | Your code must pass all linter tests.
3. **Testing** | Your code must be tested and confirmed that it does indeed do whatever it was intended to do without causing other issues.
4. **Documentation** | You must provide at least some documentation for your code, if it is a new feature we will add it to our docs.
5. **Dependencies** | If you add any new dependencies you must state it.
6. **Commit Messages** | Make sure to add a meaningful commit message that describes your changes.
7. **Branching Strategy** | Make sure to use the correct branch.
8. **Review and Discussion** | If you do not respond do reviews / discussions within a week your pull request will be cancelled.
By following these TypeScript and Deno-specific guidelines, you will help us maintain the project's code quality and streamline the contribution process. Thank you for your contributions to the CVSCode project!
Expand Down
3 changes: 3 additions & 0 deletions docs/code-examples/strLen.cvs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const string = "WADDUP"

print(strLen)
5 changes: 5 additions & 0 deletions docs/code-examples/while.cvs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var x = 12

while (x > 11) {
print("Condition met")
}
68 changes: 68 additions & 0 deletions docs/natives.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Native Functions

## How is this used?
All of these functions are built into cvsCode and can be directly called using the provided names.

## Checks
- isInt
- isPrime

## Core
- print
- println
- time

## Math
- pow
- round
- abs
- ceil
- floor
- min
- max
- exp
- sin
- cos
- tan
- asin
- acos
- atan
- sinh
- cosh
- tanh
- expm1
- log10
- sqr
- cubic
- pow10
- atan2
- hypot
- trunc
- sign
- randInt
- degToRad
- gcd
- factorial
- fibonacci
- mean
- median
- rnd
- sqrt
- slope

## Strings
- strLen
- strIncludes
- strEndsWith
- strStartsWith
- strToUppercase
- strToLowercase
- strReverse
- strTrim
- strCharAt
- strNormalize
- strTrimStart
- strTrimEnd
- strReplace
- strReplaceAll
- strRepeats
3 changes: 3 additions & 0 deletions frontend/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ export type NodeType =
| "Identifier"
| "StringLiteral";

// define our statement base
export interface Stmt {
kind: NodeType;
}

// define our program base, which consists of an array of statements
export interface Program extends Stmt {
kind: "Program",
body: Stmt[];
}

// define our variable declaration base, which consists of if its constant (cannot be changed), its identifier, and its optional value. If the value is not passed it becomes null.
export interface VarDeclaration extends Stmt {
kind: "VarDeclaration",
constant: boolean,
Expand Down
55 changes: 45 additions & 10 deletions frontend/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export enum TokenType {
// Literal Types
Number,
Identifier,
String,
String, // ""

// Keywords
Let, // let
Expand Down Expand Up @@ -48,19 +48,21 @@ export enum TokenType {
}

const KEYWORDS: Record<string, TokenType> = {
let: TokenType.Let,
const: TokenType.Const,
fn: TokenType.Fn,
if: TokenType.If,
else: TokenType.Else,
while: TokenType.While
let: TokenType.Let, // let
const: TokenType.Const, // const
fn: TokenType.Fn, // fn
if: TokenType.If, // if
else: TokenType.Else, // else
while: TokenType.While // while
}

// define the token interface, which holds the value and type for the current token.
export interface Token {
value: string,
type: TokenType,
}

// Parses the token and returns an object containing the value and the type
function token(value: string = "", type: TokenType): Token {
if (typeof value !== 'string' || !Object.values(TokenType).includes(type)) {
throw new Error('Invalid arguments for token function');
Expand All @@ -76,13 +78,15 @@ function isAlpha(char: string): boolean {
return upperCaseChar !== char.toLowerCase();
}

// Checks for skippable tokens
function isSkippable(char: string): boolean {
if (typeof char !== 'string' || char.length !== 1) {
throw new Error('Invalid argument for isSkippable function');
}
return char === ' ' || char === '\n' || char === '\t' || char === '\r';
}

// Checks for integers (1, 2, 3, ...)
function isInt(char: string): boolean {
if (typeof char !== 'string' || char.length !== 1) {
throw new Error('Invalid argument for isInt function');
Expand All @@ -92,7 +96,7 @@ function isInt(char: string): boolean {
return charCode >= bounds[0] && charCode <= bounds[1];
}


// Tokenize the source code and convert it all into tokens
export function tokenize (sourceCode: string): Token[] {
const tokens = new Array<Token>();
const src = sourceCode.split("")
Expand Down Expand Up @@ -144,70 +148,98 @@ export function tokenize (sourceCode: string): Token[] {
}
}

// parse the opening paren
else if(src[0] == '(') {
tokens.push(token(src.shift(), TokenType.OpenParen));
}

// parse the closing paren
else if (src[0] == ")") {
tokens.push(token(src.shift(), TokenType.CloseParen));
}

// parse the opening brace
else if (src[0] == "{") {
tokens.push(token(src.shift(), TokenType.OpenBrace));
}

// parse the closing brace
else if (src[0] == "}") {
tokens.push(token(src.shift(), TokenType.CloseBrace));
}

// parse the opening square bracket
else if (src[0] == "[") {
tokens.push(token(src.shift(), TokenType.OpenBracket));
}

// parse the closing square bracket
else if (src[0] == "]") {
tokens.push(token(src.shift(), TokenType.CloseBracket));
}

// parse the XOR operand
else if (src[0] == "^") {
// parse the XOR equals operand
if (src[1] == "=") {
// push the XOR equal operand
tokens.push(token(spliceFront(src, 2), TokenType.XorEqual));
} else {
// push the XOR operand as a BinaryOperator
tokens.push(token(src.shift(), TokenType.BinaryOperator));
}
}

// parse the plus operand
else if (src[0] == "+") {
// parse the increment operand
if (src[1] == "+") {
// push the incremet operand
tokens.push(token(spliceFront(src, 2), TokenType.Increment));
} else if(src[1] == '=') {
// push the += operand
tokens.push(token(spliceFront(src, 2), TokenType.PlusEquals));
} else {
// push the = as a BinaryOperator
tokens.push(token(src.shift(), TokenType.BinaryOperator));
}
}

// parse the minus operand
else if (src[0] == "-") {
// parse the decrement operand
if (src[1] == "-") {
// push the decrement operand
tokens.push(token(spliceFront(src, 2), TokenType.Decrement));
} if(src[1] == '=') {
// push the -= operand
tokens.push(token(spliceFront(src, 2), TokenType.MinusEquals));
} else {
// push the - as a BinaryOperator
tokens.push(token(src.shift(), TokenType.BinaryOperator));
}
}

// parse the multiplication operand
else if (src[0] == "*") {
// parse the *= operand
if(src[1] == '=') {
// push the *= operand
tokens.push(token(spliceFront(src, 2), TokenType.TimesEquals));
} else {
// push the = as a BinaryOperator
tokens.push(token(src.shift(), TokenType.BinaryOperator));
}
}

// parse the division operand
else if (src[0] == "/") {
// parse the /= operand
if(src[1] == '=') {
// push the /= operand
tokens.push(token(spliceFront(src, 2), TokenType.DivideEquals));
} else {
// push the / as a BinaryOperator
tokens.push(token(src.shift(), TokenType.BinaryOperator));
}
}
Expand Down Expand Up @@ -295,7 +327,9 @@ export function tokenize (sourceCode: string): Token[] {
}
}

// parse the ; symbol
else if (src[0] == ';') {
// push the ; symbol
tokens.push(token(src.shift(), TokenType.Semicolon));
}

Expand All @@ -312,8 +346,6 @@ export function tokenize (sourceCode: string): Token[] {
}

else {
// Handles multicharacter tokens

// Build number token
if(isInt(src[0])) {
let num = "";
Expand Down Expand Up @@ -355,14 +387,17 @@ export function tokenize (sourceCode: string): Token[] {
}
}

// handle the EOF (end of file)
tokens.push({
type: TokenType.EOF,
value: "EndOfFile"
});

// return our parsed tokens array
return tokens;
}

// splice the front of a string / number
function spliceFront(src: string[], n: number): string {
if (!Array.isArray(src) || typeof n !== 'number' || n < 0) {
throw new Error('Invalid arguments for spliceFront function');
Expand Down
Loading

0 comments on commit ea343e5

Please sign in to comment.