C Sharp

1747 readers
14 users here now

A community about the C# programming language

Getting started

Useful resources

IDEs and code editors

Tools

Rules

Related communities

founded 2 years ago
MODERATORS
1
 
 

Hello o/

I have been trying to get my forth interpreter working but I can't even parse it properly without error. here is what I have done so for ( it's my 3rd attempt ). if you know any good resources to learn compiler design then please link it. Thank you in advance!

namespace test;

public enum TokenType
{
    Number,
    Operator,
    String,
    Identifier,
    FunctionStart,
    FunctionEnd,
}
public class Token
{
    public TokenType Type { get; set; }
    public string Value { get; set; }

    public override string ToString()
    {
        return $"{Type}: {Value}";
    }
}

public class Program
{
    public static void Err()
    {
        Console.WriteLine("error");
    }
    public static void Main(string[] args)
    {
        string code = "12 12  : abc 10 . ;          23 - + ";

        List<Token> tokens = new List<Token>();
        bool stringMode = false;
        int startStringIndex = 0;
        for (int cur = 0; cur < code.Length; cur++)
        {
            char ch = code[cur];
            if (stringMode)
            {
                if (ch == '"' && code[cur-1] != '\\')
                {
                    stringMode = false;
                    string strValue = code.Substring(startStringIndex, cur - startStringIndex);
                    tokens.Add(new Token { Type = TokenType.String, Value = strValue });
                }
            }
            switch (ch)
            {
                case '+':
                case '-':
                case '*':
                case '/':
                    {
                        tokens.Add(new Token { Type = TokenType.Operator, Value = ch.ToString() });
                        break;
                    }
                case '.':
                    {
                        if (cur + 1 < code.Length && code[cur+1] == '"') 
                        {
                            stringMode = true;
                            cur+=2; // Skip the opening quote
                            startStringIndex = cur;
                        } else
                        {
                            tokens.Add(new Token { Type = TokenType.Operator, Value = ch.ToString() });
                        }
                        break;
                    }
                case >= '0' and <= '9':
                    {
                        int start = cur;
                        while (cur + 1 < code.Length && code[cur + 1] >= '0' && code[cur + 1] <= '9')
                        {
                            cur++;
                        }
                        string numberValue = code.Substring(start, cur - start + 1);
                        tokens.Add(new Token { Type = TokenType.Number, Value = numberValue });
                        break;
                    }
                case (>= 'a' and <= 'z') or (>= 'A' and <= 'Z'):
                    {
                        int start = cur;
                        while (cur + 1 < code.Length && ((code[cur + 1] >= 'a' && code[cur + 1] <= 'z') || (code[cur + 1] >= 'A' && code[cur + 1] <= 'Z') || (code[cur + 1] >= '0' && code[cur + 1] <= '9') || code[cur + 1] == '_'))
                        {
                            cur++;
                        }
                        string identifierValue = code.Substring(start, cur - start + 1);
                        tokens.Add(new Token { Type = TokenType.Identifier, Value = identifierValue });
                        break;
                    }
                case ':':
                    {
                        tokens.Add(new Token { Type = TokenType.FunctionStart, Value = ch.ToString() });
                        break;
                    }
                case ';':
                    {
                        tokens.Add(new Token { Type = TokenType.FunctionEnd, Value = ch.ToString() });
                        break;
                    }
                case ' ':
                case '\n':
                    {
                        break;
                    }
                default:
                    {
                        Err();
                        Console.WriteLine("Something went wrong!");
                        break;
                    }
            }
        }

        foreach (var token in tokens)
        {
            Console.WriteLine(token);
        }
    }
}

2
 
 

Hello,

I have been trying to learn c# from dotnet tutorial and they gave this code which supposed to trigger garbage collector and in turn call the Destructor of the class. however when I run the same code, I get result of Constructor only and not the Destructor.

using System;
namespace DestructorExample
{
    class DestructorDemo
    {
        public DestructorDemo()
        {
            Console.WriteLine("Constructor Object Created");
        }
        ~DestructorDemo()
        {
            string type = GetType().Name;
            Console.WriteLine($"Object {type} is Destroyed");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            DestructorDemo obj1 = new DestructorDemo();
            DestructorDemo obj2 = new DestructorDemo();

            //Making obj1 for Garbage Collection
            obj1 = null;
            GC.Collect();
            Console.ReadKey();
        }
    }
}

here is the code, if you know please tell me why it's not working

3
 
 

If you find any mistake kindly inform me :)

4
5
6
-1
Collections in c# (ghodawalaaman.blogspot.com)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
view more: next ›