Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7007005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:33:03+00:00 2026-05-27T21:33:03+00:00

I’m working with Coco R sample Taste, in C#. I’m trying to expand the

  • 0

I’m working with Coco R sample Taste, in C#.

I’m trying to expand the code to write Strings when an operation is given such as

write hello world

I have identified a way, in which I can store and write strings. I’m putting out relevant code for the problem I’m having:

Expanded Taste.ATG

| "write" 
    { Expr<out type>    (. if (type != integer) SemErr("integer type expected");
                            gen.Emit(Op.WRITE); .)
    | '"' 
    ident       (. name = Convert.ToString(t.val);
                            gen.Emit(Op.READS);
                            gen.Emit(Op.WRITES).)
    '"'
    }';'

Expanded Operations in CodeGen.cs:
Filestream is used in this way

public void Interpret (string data) { 
    int val;
    try {
        FileStream s = new FileStream(data, FileMode.Open);
        Console.WriteLine();
        pc = progStart; stack[0] = 0; top = 1; bp = 0;

and added case switches, that use filestream

case Op.READ:  val = ReadInt(s); Push(val); break;
case Op.READS: stackString[index] = ReadString(s) ; Push(index); index++; break;
case Op.WRITE: Console.WriteLine(Pop()); break;
case Op.WRITES: Console.WriteLine(stackString[Pop()]); break;

The problem is, I cant find anywhere on the internet a way to read a String,
clearly ReadString(s) doesn’t work the same way ReadInt(s) does.
I was wondering if I can get help to find an operation, that reads a string from the file stream.

I haven’t done any filestream management before.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:33:04+00:00Added an answer on May 27, 2026 at 9:33 pm

    EDIT3
    After looking into this stuff again, I found a bigger problem with this approach. First off some explanation: Coco/R generates the scanner and parser from the atg file, the main program in Taste.cs uses these to compile the Taste.TAS.

    The compiled Taste.TAS is then fed into the CodeGen.cs’s Interpret method which works down the opcodes it receives like a virtual machine, so it’s ReadInt() method is supposed to read from Taste.IN, which contains sample data for the compiled Taste.TAS-program.

    So, to add support for a hello world in the CodeGen.cs, it is not enough to change the Interpret method, you will have to patch the Emit method as well, to allow the compiler to add the string at compile-time.

    Hacky as always (goes into CodeGen.cs):

    List<string> strStack = new List<string>();
    public void Emit(Op op, string str)
    {
        int idx = strStack.Count;
        strStack.Add(str);
        Emit(op, idx); // adds the opcode, 
    }
    

    In the Taste.ATG you will have to change the Write-instruction to Gen.Emit(Op.WRITES, t.val);

    And in the Interpret-method, you will need to use the reference to the string list:

    case Op.WRITES: Console.WriteLine(strStack[Next2()]); break;
    

    EDIT4 – Just for future reference To read a string literal from a file you could use the StreamReader class like so:

        /// <summary>
        ///     Reads a string literal from a file, essentially implementing the regex pattern /\"{.*}\"/.
        ///     Ignores escape characters (for instance, "\"" will fail)
        /// </summary>
        /// <param name="fs">The file stream to read from.</param>
        /// <returns>The string literal without it's quotes upon success, null otherwise.</returns>
        static string ReadString(FileStream fs)
        {
            if (!fs.CanRead)
                return null; // cant read from stream, throw an exception here
    
            var reader = new StreamReader(fs);
            var sb = new StringBuilder();
    
            bool inString = false;
    
            while (true)
            {
                if (reader.Peek() < 0)
                    return null; // reached EOF before string ended, throw exception here
    
                char ch = (char)reader.Read();
    
                if (inString)
                    if (ch == '"')
                        break;
                    else
                        sb.Append(ch);
                else
                    if (ch == '"')
                        inString = true;
                    else if (!char.IsWhiteSpace(ch))
                        return null; // string does not start with quote, throw exception here
            }
    
            return sb.ToString();
        }
    

    An alternative would be to use the [Regex][3] class, but since it by default only works with strings, it requires some tricky read-and-seek operations to get strings spanning multiple lines (if supported), so you do not fubar the filestream for the rest of the program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.